package com.wangku.was.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import net.sf.json.JSONObject;
/**
* 根据纬度 经度值获取用户的地理位置
* @ClassName: LatitudeUtil
* @author zhangmeijie
* @date 2015-6-29 下午1:44:44
* @exception
* @since 1.0
*/
public class LatitudeUtil {
/**
* @Title: geocodeAddr
* @param latitude 纬度
* @param longitude 经度
* @return
* @author zhangmeijie
* @date 2015-6-29 下午1:42:24
* @exception
* @since 1.0
*/
@SuppressWarnings("static-access")
public static String getAddressByLatAndLon(String latitude, String longitude) {
String address = null;
String url = String.format("http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=%s,%s&output=json&pois=1", latitude, longitude);
try {
URL myURL = new URL(url);
URLConnection httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
StringBuffer sb = new StringBuffer("");
while ((data = br.readLine()) != null) {
sb.append(data);
}
JSONObject jsonObject = new JSONObject().fromObject(sb.toString());
address = jsonObject.getJSONObject("result").getString("formatted_address");
insr.close();
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return address;
}
/**
* 测试方法
* @Title: main
* @param
* @return
* @author zhangmeijie
* @date 2015-6-29 下午1:44:50
* @exception
* @since 1.0
*/
public static void main(String[] args) {
String addr = getAddressByLatAndLon("22.53836564", "114.02681090");// 纬度 经度
System.out.println(addr);
}
}