1. 程式人生 > >利用百度 Geocoding 位置、經緯度互轉

利用百度 Geocoding 位置、經緯度互轉

1、利用位置得到經緯度:

String url="http://api.map.baidu.com/geocoder/v2/?address=" +place1+
                "&ak=cev92xxxxxxxxx4HibK&callback=showLocation&" +
                "output=json&pois=0&mcode=" +              "9F:F9:BF:E5:B1:FF:71:2C:xxxxxxxxxxxxxxxxxxx;com.xxxxxxx.xxxxxxxx";

利用get網路請求這個url;
其中 place1:地址;
ak:開發者平臺的應用ak;
mcode:安全碼;
最後是包名;

成功返回:

showLocation&&showLocation({"status":0,"result":{"location":{"lng":120.1609994741205,"lat":30.26998128653152},"precise":1,"confidence":80,"level":"道路"}})

解析得到 經緯度。
解析這個就很納悶 我只想到了

String str2=str.replace("showLocation&&showLocation(","");
String str3=str2.replace(")","");

然後後面就JSONObject 了。好low啊 沒想到更好的辦法了。。。

2、傳經緯度 得到位置

String url3="http://api.map.baidu.com/geocoder/v2/?ak=xxxxxxxxxxxxxxxxxxxxx&callback=renderReverse&location=39.983424,116.322987&output=json&pois=1&mcode=" +
        "9F:F9:BF:E5:B1:FF:71:2C:ED:5B:6Axxxxxxxxxxxxxxxxx;com.xxxxxx.xxxxxx";

替換了
callback=showLocation —->callback=renderReverse
address=位置—->location =經緯度