一 创建百度地图api应用
地址:http://lbsyun.baidu.com/apiconsole/key/create
创建成功后
public static Object getLngAndLat(String pickAddress) {
Object location = "";
pickAddress = pickAddress.replace(" ", "");
String yourAk = "创建应用的ak";
String url = "http://api.map.baidu.com/geocoder?address="+ pickAddress +"&output=json&key="+ yourAk;
try {
String json = loadJSON(url);
JSONObject obj = JSONObject.parseObject(json);
location = obj;
} catch (Exception e) {
System.out.println("未找到相匹配的经纬度,请检查地址!");
}
return location;
}
public static String loadJSON(String url) {
StringBuilder json = new StringBuilder();
try {
URL oracle = new URL(url);
URLConnection yc = oracle.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 (MalformedURLException e) {
} catch (IOException e) {
}
return json.toString();
}
测试
@GetMapping("/longitude/{pickAddress}")
public Object location (@PathVariable String pickAddress) {
Object location = getLngAndLat(pickAddress);
return location;
}