當前位置:首頁 » 編程語言 » java導航

java導航

發布時間: 2023-10-25 09:42:30

Ⅰ 怎樣在java中調用百度地圖的API

網路地圖Web服務API為開發者提供http介面,即開發者通過http形式發起檢索請求,獲取返回json或xml格式的檢索數據。
api官網說明鏈接:

http://developer..com/map/webservice.htm

1、可用介面列舉:
獲取相關地址提示 place suggestion
http://api.map..com/place/v2/suggestion?query=水杉®ion=成都&output=json&ak=

2、獲取坐標 geocoding

http://api.map..com/geocoder?address=錦江區菱安路299號&output=json&key=

http://api.map..com/place/v2/search?ak=&output=json&query=藍光coco金沙&page_size=10&page_num=0&scope=1®ion=成都
{
"status":0,
"message":"ok",
"total":2,
"results":[
{
"name":"藍光COCO金沙",
"location":{
"lat":30.687544,
"lng":103.996691
},
"address":"西三環外金沙西源大道(IT大道旁)",
"uid":"223992992c5ee7e0841541df"
},
{
"name":"藍光COCO金沙2期",
"location":{
"lat":30.681123,
"lng":103.991123
},
"address":"青羊區金沙IT大道旁",
"uid":"b7bb5abb1cd4982213293580"
}
]
}

3、設施導航
http://api.map..com/place/v2/search?ak=&output=json&query=圖書館&page_size=20&page_num=0&scope=2®ion=成都

4、java測試類:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class LocationUtil {

private static final String BAIDU_APP_KEY = "";

/**
* 返回輸入地址的經緯度坐標 key lng(經度),lat(緯度)
*/
public static Map<String, String> getLatitude(String address) {
try {
// 將地址轉換成utf-8的16進制
address = URLEncoder.encode(address, "UTF-8");
// 如果有代理,要設置代理,沒代理可注釋
// System.setProperty("http.proxyHost","192.168.172.23");
// System.setProperty("http.proxyPort","3209");

URL resjson = new URL("http://api.map..com/geocoder?address="
+ address + "&output=json&key=" + BAIDU_APP_KEY);
BufferedReader in = new BufferedReader(new InputStreamReader(
resjson.openStream()));
String res;
StringBuilder sb = new StringBuilder("");
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
in.close();
String str = sb.toString();
System.out.println("return json:" + str);
if(str!=null&&!str.equals("")){
Map<String, String> map = null;
int lngStart = str.indexOf("lng\":");
int lngEnd = str.indexOf(",\"lat");
int latEnd = str.indexOf("},\"precise");
if (lngStart > 0 && lngEnd > 0 && latEnd > 0) {
String lng = str.substring(lngStart + 5, lngEnd);
String lat = str.substring(lngEnd + 7, latEnd);
map = new HashMap<String, String>();
map.put("lng", lng);
map.put("lat", lat);
return map;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void main(String args[]) {

Map<String, String> map = LocationUtil.getLatitude("成都 高新西區西區大道1398號");
if (null != map) {
System.out.println(map.get("lng"));
System.out.println(map.get("lat"));
}
}
}

熱點內容
yeah郵箱的伺服器地址 發布:2025-09-16 23:36:52 瀏覽:701
c的引用java 發布:2025-09-16 23:36:48 瀏覽:307
的n次方編程 發布:2025-09-16 23:25:34 瀏覽:285
python安卓版 發布:2025-09-16 23:01:04 瀏覽:823
碼小易編程 發布:2025-09-16 23:00:56 瀏覽:335
在線音樂源碼 發布:2025-09-16 22:57:39 瀏覽:685
api開發php 發布:2025-09-16 22:06:15 瀏覽:600
mysql自動備份linux 發布:2025-09-16 21:58:33 瀏覽:947
怎麼用自己的伺服器ip做域名 發布:2025-09-16 21:49:57 瀏覽:919
vc為什麼能編譯不能用 發布:2025-09-16 21:48:03 瀏覽:748