根据地址获取经纬度
前提需要先去官方注册一个账号搞一个key过来
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public String getLngLat(String address) {
StringBuffer json = new StringBuffer();
try {
URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=???");
URLConnection yc = u.openConnection();
//读取返回的数据
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));
String inputline = null;
while((inputline=in.readLine())!=null){
json.append(inputline);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
String jsonStr=json.toString();
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
if(jsonObject.getJSONArray("geocodes").size()>0)
return jsonObject.getJSONArray("geocod es").getJSONObject(0).get("location").toString();
else
return null;
}
private static double rad(double d){
return d * Math.PI / 180.0;
}
***调用一下***
public static void main(String[] args) {
MapUtil addressLngLatExchange=new MapUtil();
System.out.println(addressLngLatExchange.getLngLat("北京高碑店地铁站"));
}