百度api调用
/**
* description 百度地图逆坐标解析
*
* @param
* @return
* @exception/throws
* @date 2020/10/22 上午10:13
*/
public static LngAndLat getBaiduLngAndLat(String address) {
LngAndLat lngAndLat = new LngAndLat();
String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=yourak";
String json = loadJSON(url);
JSONObject obj = JSONObject.fromObject(json);
if (obj.get("status").toString().equals("0")) {
double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
lngAndLat.setLat(lat);
lngAndLat.setLng(lng);
} else {
}
return lngAndLat;
}
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()));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (IOException ignored) {
}
return json.toString();
}
腾讯api调用
/**
* description 腾讯逆坐标解析
* @param
* @return 火星坐标系
* @exception/throws
* @date 2020/10/22 上午10:14
*/
public static LngAndLat getQQLngANdLat(String address) {
RestTemplate restTemplate=new RestTemplate();
String url="https://apis.map.qq.com/ws/geocoder/v1/?address="+address+"®ion=北京"+
"&key=yourkey";
JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
System.out.println(forObject);
LngAndLat lngAndLat=new LngAndLat();
double lng = forObject.getJSONObject("result").getJSONObject("location").getDouble("lng");
double lat = forObject.getJSONObject("result").getJSONObject("location").getDouble("lat");
lngAndLat.setLat(lat);
lngAndLat.setLng(lng);
return lngAndLat;
}
完整类
public class LngAndLatUtil {
/**
* description 百度地图逆坐标解析
*
* @param
* @return
* @exception/throws
* @date 2020/10/22 上午10:13
*/
public static LngAndLat getBaiduLngAndLat(String address) {
LngAndLat lngAndLat = new LngAndLat();
String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=yourak";
String json = loadJSON(url);
JSONObject obj = JSONObject.fromObject(json);
if (obj.get("status").toString().equals("0")) {
double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
lngAndLat.setLat(lat);
lngAndLat.setLng(lng);
} else {
}
return lngAndLat;
}
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()));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (IOException ignored) {
}
return json.toString();
}
/**
* description 腾讯逆坐标解析
* @param
* @return 火星坐标系
* @exception/throws
* @date 2020/10/22 上午10:14
*/
public static LngAndLat getQQLngANdLat(String address) {
RestTemplate restTemplate=new RestTemplate();
String url="https://apis.map.qq.com/ws/geocoder/v1/?address="+address+"®ion=北京"+
"&key=yourkey";
JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
System.out.println(forObject);
LngAndLat lngAndLat=new LngAndLat();
double lng = forObject.getJSONObject("result").getJSONObject("location").getDouble("lng");
double lat = forObject.getJSONObject("result").getJSONObject("location").getDouble("lat");
lngAndLat.setLat(lat);
lngAndLat.setLng(lng);
return lngAndLat;
}
public static void main(String[] args) {
LngAndLat lngAndLat = LngAndLatUtil.getQQLngANdLat("北京故宫");
System.out.println("(经度:" + lngAndLat.getLng() + ",纬度:" + lngAndLat.getLat() + ")");
}
@Data
public static class LngAndLat {
private Double lng;
private Double lat;
}
}
目录