百度地图和高德地图用户获取国内地址,
区别在于高德地图的精确度比百度地图要高一点,
百度经纬度和高德地图经纬度位置相反,
高德地图不支持国外地址,
ArcGIS地图获取国外地址
1.首先需要到高德开发平台申请key
地址:https://lbs.amap.com/api/webservice/guide/api/georegeo
使用百度地图的 需要去百度开发平台申请ak
百度地图:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
高德和百度申请步骤都一样
首先需要注册登录
登录成功之后点击控制台==》点击应用管理==》点击我的应用
然后点击创建应用就可以申请到key或者ak了
2.项目中加依赖
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
依赖下载地址:https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5
3.封装好的实体类
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author: YXY
* @date: 2021/1/22 11:22
* @Version 1.0
* 位置信息
*/
@Data
public class CommonLocation {
@ApiModelProperty("省")
private String province;
@ApiModelProperty("市")
private String district;
@ApiModelProperty("区")
private String city;
@ApiModelProperty("详细地址")
private String address;
}
4.封装好的工具类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* 逆地理编码
* @author: YXY
* @date: 2021/01/22
*/
public class ReverseGeocoding {
/**
*高德地图API
* @param longitude
* @param latitude
* @return
*/
public static CommonLocation GetLocationMsg(double longitude, double latitude) {
String message = "";
String address = "";
String province = "";
String district = "";
String city = "";
// 高德地图逆地理编码API
String url = String.format(
"https://restapi.amap.com/v3/geocode/regeo?output=JSON&key=自己的key&radius=1000&extensions=all&batch=false&roadlevel=0&location=%s,%s",
longitude, latitude);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpsConn = (URLConnection) myURL.openConnection();
httpsConn.setConnectTimeout(100000);
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
while ((data = br.readLine()) != null) {
message = message + data;
}
JsonParser jp = new JsonParser();
//将json字符串转化成json对象
JsonObject jo = jp.parse(message).getAsJsonObject();
String status = jo.get("status").getAsString();
String addressJsonEle = jo.get("regeocode").getAsJsonObject().get("formatted_address").toString();
JsonObject addressComponentJson = jo.get("regeocode").getAsJsonObject().get("addressComponent").getAsJsonObject();
//省市区
//.replace("\"","") 是将返回的""北京市"" 变成 "北京市"
province = addressComponentJson.get("province").toString().replace("\"","");
district = addressComponentJson.get("district").toString().replace("\"","");
city = addressComponentJson.get("city").toString().replace("\"","");
if (addressJsonEle.equals("[]")) {
address = null;
} else {
if (jo.get("regeocode").getAsJsonObject().get("pois").getAsJsonArray().size() <= 0) {
String detail = jo.get("regeocode").getAsJsonObject().get("addressComponent").getAsJsonObject().get("streetNumber").getAsJsonObject().get("street").getAsString() + jo.get("regeocode").getAsJsonObject().get("addressComponent").getAsJsonObject().get("streetNumber").getAsJsonObject().get("number").getAsString();
if (status.equals("1") && !addressJsonEle.equals("[]")) {
address = addressJsonEle + " " + detail;
}
} else {
String detail = jo.get("regeocode").getAsJsonObject().get("pois").getAsJsonArray().get(0).getAsJsonObject().get("name").getAsString();
String detailDistance = jo.get("regeocode").getAsJsonObject().get("pois").getAsJsonArray().get(0).getAsJsonObject().get("distance").getAsString();
if (status.equals("1") && !addressJsonEle.equals("[]")) {
address = addressJsonEle + " " + detail + " " + detailDistance.substring(0, detailDistance.lastIndexOf(".")) + "米";
}
}
}
insr.close();
}
} catch (
IOException e) {
e.printStackTrace();
}
CommonLocation result = new CommonLocation();
if(address !=null){
result.setAddress(address);
}
if(province !=null){
result.setProvince(province);
if(city.equals("[]")){
result.setCity(province);
}
}
if(district !=null){
result.setDistrict(district);
}
if(!city.equals("[]")){
result.setCity(city);
}
return result;
}
/**
*百度地图aPI
* @param longitude
* @param latitude
* @return
*/
public static String GetLocationMsgs(double latitude, double longitude) {
String message = "";
String address = "";
// 百度地图逆地理编码API
String url = String.format(
"http://api.map.baidu.com/reverse_geocoding/v3/?ak=自己的ak&extensions_poi=1&radius=1000&output=json&coordtype=bd09ll&location=%s,%s",
latitude, longitude);
//String location=longitude+","+latitude;
//wgs84坐标系(GPS经纬度/GPS传感器获得的GPS数据一般用这个)
//String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll&location="+location;
//gcj02坐标系(国测局经纬度坐标)
//String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=gcj02ll&location="+location;
//bd09坐标系(百度经纬度坐标)
//String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=bd09ll&location="+location;
//bd09mc坐标系(百度米制坐标)
//String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=bd09mc&location="+location;
//System.out.println(url); //打印url,可访问url获得全部的Json数据体,可按需提取数据
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
while ((data = br.readLine()) != null) {
message = message + data;
}
JsonParser jp = new JsonParser();
//将json字符串转化成json对象
JsonObject jo = jp.parse(message).getAsJsonObject();
String status = jo.get("status").getAsString();
if (jo.get("result").getAsJsonObject().get("pois").getAsJsonArray().size() <= 0) {
String adds = jo.get("result").getAsJsonObject().get("formatted_address").getAsString();
address = adds;
} else {
JsonElement addressJsonEle = jo.get("result").getAsJsonObject().get("addressComponent");
String adds = jo.get("result").getAsJsonObject().get("formatted_address").getAsString();
String details = jo.get("result").getAsJsonObject().get("pois").getAsJsonArray().get(0).getAsJsonObject().get("name").getAsString() + " " +
jo.get("result").getAsJsonObject().get("pois").getAsJsonArray().get(0).getAsJsonObject().get("distance").getAsString() + "米";
if (status.equals("0")) {
address = adds + " " + details;
}
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return address;
}
/**
*ArcGIS地图
* @param longitude
* @param latitude
* @return
*/
public static String GetLocationMsgForForeign(double longitude, double latitude) {
String message = "";
String address = "";
// ArcGIS地图逆地理编码API
String url = String.format(
"http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode?f=pjson&langCode=EN&featureTypes=&location=%s,%s",
longitude, latitude);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpsConn = (URLConnection) myURL.openConnection();
httpsConn.setConnectTimeout(100000);
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
while ((data = br.readLine()) != null) {
message = message + data;
}
JsonParser jp = new JsonParser();
//将json字符串转化成json对象
JsonObject jo = jp.parse(message).getAsJsonObject();
if (!jo.has("error")) {
address = jo.get("address").getAsJsonObject().get("LongLabel").getAsString();
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return address;
}
public final static void main(String[] args) {
GetLocationMsg(116.23128, 40.22077);
CommonLocation commonLocation = GetLocationMsg(116.23128, 40.22077);
//String baiduResult = GetLocationMsgs( 40.22077, 116.23128);
//String result = GetLocationMsgForForeign(116.23128, 40.22077);
System.out.println("高德地址===" +commonLocation);
//System.out.println("百度地址===" + baiduResult);
//System.out.println("国际地址===" + result);
}
}