分享一个关于Java调用百度、高德API、ArcGIS地图获取逆地理的代码
百度地圖和高德地圖用戶獲取國(guó)內(nèi)地址,
區(qū)別在于高德地圖的精確度比百度地圖要高一點(diǎn),
百度經(jīng)緯度和高德地圖經(jīng)緯度位置相反,
高德地圖不支持國(guó)外地址,
ArcGIS地圖獲取國(guó)外地址
1.首先需要到高德開(kāi)發(fā)平臺(tái)申請(qǐng)key
地址:https://lbs.amap.com/api/webservice/guide/api/georegeo
使用百度地圖的 需要去百度開(kāi)發(fā)平臺(tái)申請(qǐng)ak
百度地圖:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
高德和百度申請(qǐng)步驟都一樣
首先需要注冊(cè)登錄
登錄成功之后點(diǎn)擊控制臺(tái)==》點(diǎn)擊應(yīng)用管理==》點(diǎn)擊我的應(yīng)用
然后點(diǎn)擊創(chuàng)建應(yīng)用就可以申請(qǐng)到key或者ak了
2.項(xiàng)目中加依賴
<!-- 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.封裝好的實(shí)體類
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 = "";// 高德地圖逆地理編碼APIString 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字符串轉(zhuǎn)化成json對(duì)象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();//省市區(qū)//.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 = "";// 百度地圖逆地理編碼APIString 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坐標(biāo)系(GPS經(jīng)緯度/GPS傳感器獲得的GPS數(shù)據(jù)一般用這個(gè))//String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll&location="+location;//gcj02坐標(biāo)系(國(guó)測(cè)局經(jīng)緯度坐標(biāo))//String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=gcj02ll&location="+location;//bd09坐標(biāo)系(百度經(jīng)緯度坐標(biāo))//String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=bd09ll&location="+location;//bd09mc坐標(biāo)系(百度米制坐標(biāo))//String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=bd09mc&location="+location;//System.out.println(url); //打印url,可訪問(wèn)url獲得全部的Json數(shù)據(jù)體,可按需提取數(shù)據(jù)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字符串轉(zhuǎn)化成json對(duì)象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地圖逆地理編碼APIString 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字符串轉(zhuǎn)化成json對(duì)象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("國(guó)際地址===" + result);}} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的分享一个关于Java调用百度、高德API、ArcGIS地图获取逆地理的代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。