?
前言
要調(diào)用百度地圖API,步驟操作如下
- 注冊并創(chuàng)建一個API密鑰。您可以在百度地圖API控制臺上創(chuàng)建您的密鑰。
- 選擇要使用的API服務(wù)。百度地圖API提供了多種服務(wù),包括地圖展示、路線規(guī)劃、地點搜索、實時交通等。您可以在百度地圖API控制臺上查看所有可用的服務(wù)。
- 在調(diào)用API時,將API密鑰添加到請求中。
官網(wǎng):百度地圖開放平臺 | 百度地圖API SDK
1、了解地圖開放平臺
?可以看到開發(fā)文檔支持多種類型,前端到后臺等等。百度地圖API提供了多種服務(wù),包括地圖展示、路線規(guī)劃、地點搜索、鷹眼軌跡,定位,實時交通等。
?2、創(chuàng)建百度地圖AK
2.1、控制臺→應(yīng)用管理→我的應(yīng)用→創(chuàng)建應(yīng)用
2.2、創(chuàng)建應(yīng)用名字
?為什么高級是灰色選不了,是因為高級服務(wù)是需要付費的。
2.4、IP白名單設(shè)置
?2.5、查看AK
3、JAVA調(diào)用百度地圖
3.1、地點檢索
3.1.1、調(diào)用百度Api?
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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 java.text.DecimalFormat;
import java.util.Map;
public class BaiduApiController {
public static void main(String[] args) {
String ak = "自己AK";
String address = "天津之眼摩天輪";
// String httpUrl = "http://api.map.baidu.com/geocoding/v3/?address="+address+ ak;
String httpUrl = "http://api.map.baidu.com/geocoding/v3/?address="+address+"&output=json&ak=" + ak;
StringBuilder json = new StringBuilder();
try {
URL url = new URL(httpUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
System.out.println(json.toString());
String data = json.toString();
if (data != null && !"".equals(data)) {
Map map = JSON.parseObject(data, Map.class);
if ("0".equals(map.getOrDefault("status", "500").toString())) {
Map childMap = (Map) map.get("result");
Map posMap = (Map) childMap.get("location");
double lng = Double.parseDouble(posMap.getOrDefault("lng", "0").toString()); // 經(jīng)度
double lat = Double.parseDouble(posMap.getOrDefault("lat", "0").toString()); // 緯度
DecimalFormat df = new DecimalFormat("#.######");
String lngStr = df.format(lng);
String latStr = df.format(lat);
String result = lngStr + "," + latStr;
System.out.println("坐標(biāo)"+result);
}
}
}
}
3.1.2、查看結(jié)果
3.1.3、驗證
3.2、計算兩點之間距離
3.2.1、調(diào)用百度Api?
mport com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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 java.text.DecimalFormat;
import java.util.Map;
/**
* 百度地圖操作工具類
*/
public class BaiduApiUtils {
public static void main(String[] args) {
String origin = getCoordinate("天津之眼摩天輪");
String destination = getCoordinate("北京市百度大廈");
Double distance = getDistance(origin, destination);
System.out.println("訂單距離:" + distance + "米");
Integer time = getTime(origin, destination);
System.out.println("線路耗時" + time + "秒");
}
private static String AK = "自己AK";
/**
* 調(diào)用百度地圖地理編碼服務(wù)接口,根據(jù)地址獲取坐標(biāo)(經(jīng)度、緯度)
*
* @param address
* @return
*/
public static String getCoordinate(String address) {
String httpUrl = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + AK;
String json = loadJSON(httpUrl);
Map map = JSON.parseObject(json, Map.class);
String status = map.get("status").toString();
if (status.equals("0")) {
//返回結(jié)果成功,能夠正常解析地址信息
Map result = (Map) map.get("result");
Map location = (Map) result.get("location");
String lng = location.get("lng").toString();
String lat = location.get("lat").toString();
DecimalFormat df = new DecimalFormat("#.######");
String lngStr = df.format(Double.parseDouble(lng));
String latStr = df.format(Double.parseDouble(lat));
String r = latStr + "," + lngStr;
return r;
}
return null;
}
/**
* 調(diào)用百度地圖駕車路線規(guī)劃服務(wù)接口,根據(jù)寄件人地址和收件人地址坐標(biāo)計算訂單距離
*
* @param origin
* @param destination
* @return
*/
public static Double getDistance(String origin, String destination) {
String httpUrl = "http://api.map.baidu.com/directionlite/v1/driving?origin="
+ origin + "&destination="
+ destination + "&ak=" + AK;
String json = loadJSON(httpUrl);
Map map = JSON.parseObject(json, Map.class);
if ("0".equals(map.getOrDefault("status", "500").toString())) {
Map childMap = (Map) map.get("result");
JSONArray jsonArray = (JSONArray) childMap.get("routes");
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
double distance = Double.parseDouble(jsonObject.get("distance") == null ? "0" : jsonObject.get("distance").toString());
return distance;
}
return null;
}
/**
* 調(diào)用百度地圖駕車路線規(guī)劃服務(wù)接口,根據(jù)寄件人地址和收件人地址坐標(biāo)計算訂單距離
*
* @param origin
* @param destination
* @return
*/
public static Integer getTime(String origin, String destination) {
String httpUrl = "http://api.map.baidu.com/directionlite/v1/driving?origin="
+ origin + "&destination="
+ destination + "&ak=" + AK;
String json = loadJSON(httpUrl);
Map map = JSON.parseObject(json, Map.class);
if ("0".equals(map.getOrDefault("status", "500").toString())) {
Map childMap = (Map) map.get("result");
JSONArray jsonArray = (JSONArray) childMap.get("routes");
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
int time = Integer.parseInt(jsonObject.get("duration") == null ? "0" : jsonObject.get("duration").toString());
return time;
}
return null;
}
/**
* 調(diào)用服務(wù)接口,返回百度地圖服務(wù)端的結(jié)果
*
* @param httpUrl
* @return
*/
public static String loadJSON(String httpUrl) {
StringBuilder json = new StringBuilder();
try {
URL url = new URL(httpUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
System.out.println(json.toString());
return json.toString();
}
}
3.2.2、查看結(jié)果
3.3、地點輸入提示
3.3.1、調(diào)用百度Api?
/**
* 選擇了ak或使用IP白名單校驗:
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
public class SearchHttpAK {
public static String URL = "https://api.map.baidu.com/place/v2/suggestion?";
public static String AK = "自己AK";
public static void main(String[] args) throws Exception {
SearchHttpAK snCal = new SearchHttpAK();
Map params = new LinkedHashMap<String, String>();
params.put("query", "天安門");
params.put("region", "北京");
params.put("city_limit", "true");
params.put("output", "json");
params.put("ak", AK);
snCal.requestGetAK(URL, params);
}
/**
* 默認(rèn)ak
* 選擇了ak,使用IP白名單校驗:
* 根據(jù)您選擇的AK以為您生成調(diào)用代碼
* 檢測到您當(dāng)前的ak設(shè)置了IP白名單校驗
* 您的IP白名單中的IP非公網(wǎng)IP,請設(shè)置為公網(wǎng)IP,否則將請求失敗
* 請在IP地址為0.0.0.0/0 外網(wǎng)IP的計算發(fā)起請求,否則將請求失敗
*/
public void requestGetAK(String strUrl, Map<String, String> param) throws Exception {
if (strUrl == null || strUrl.length() <= 0 || param == null || param.size() <= 0) {
return;
}
StringBuffer queryString = new StringBuffer();
queryString.append(strUrl);
for (Map.Entry<?, ?> pair : param.entrySet()) {
queryString.append(pair.getKey() + "=");
queryString.append(URLEncoder.encode((String) pair.getValue(),
"UTF-8") + "&");
}
if (queryString.length() > 0) {
queryString.deleteCharAt(queryString.length() - 1);
}
java.net.URL url = new URL(queryString.toString());
System.out.println(queryString.toString());
URLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.connect();
InputStreamReader isr = new InputStreamReader(httpConnection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
isr.close();
System.out.println("AK: " + buffer.toString());
}
}
?3.3.2、查看結(jié)果
4、接口說明
4.1、接口文檔說明
- 開發(fā)文檔有很多案例類型,可根據(jù)項目業(yè)務(wù)需求進(jìn)行選擇性調(diào)用。
4.2、請求參數(shù)?
- 有些參數(shù)是必填就比如開發(fā)者密鑰AK ,這個參數(shù)沒有都請求不到數(shù)據(jù)。
字段名稱 | 字段含義 | 字段類型 | 必填 | 備注 |
---|---|---|---|---|
ak | 開發(fā)者密鑰,AK申請 |
string | 是 | |
origin | 起點 | double,double | 是 | 起點經(jīng)緯度,格式為:緯度,經(jīng)度;小數(shù)點后不超過6位,40.056878,116.30815 |
destination | 終點 | double,double | 是 | 終點經(jīng)緯度,格式為:緯度,經(jīng)度;小數(shù)點后不超過6位,40.056878,116.30815 |
origin_uid | 起點uid,POI 的 uid(在已知起點POI 的 uid 情況下,請盡量填寫uid,將提升路線規(guī)劃的準(zhǔn)確性) | string | 否 | |
destination_uid | 終點uid,POI 的 uid(在已知終點POI 的 uid 情況下,請盡量填寫uid,將提升路線規(guī)劃的準(zhǔn)確性) | string | 否 | |
riding_type | 騎行類型 | string | 否 | 默認(rèn)0 |
coord_type | 輸入坐標(biāo)類型 | string | 否 | 默認(rèn)bd09ll |
ret_coordtype | 輸出坐標(biāo)類型 | string | 否 | 返回值的坐標(biāo)類型,默認(rèn)為百度經(jīng)緯度坐標(biāo):bd09ll |
sn | 用戶的權(quán)限簽名,當(dāng)AK設(shè)置為SN校驗時,該參數(shù)必填SN計算方法 |
string | 否 | |
timestamp | 時間戳,與SN配合使用 | string | SN存在時必填 | |
steps_info | 是否下發(fā)step詳情 |
int | 否 |
?4.3、返回參數(shù)?
- 返回的狀態(tài) ,錯誤碼等等都寫的非常詳細(xì)。
字段名稱 | 字段含義 | 備注 | |||
---|---|---|---|---|---|
status | 狀態(tài)碼 | 0:成功 1:服務(wù)內(nèi)部錯誤 2:參數(shù)無效 7:無返回結(jié)果 |
|||
message | 狀態(tài)碼對應(yīng)的信息 | ||||
result | 返回的結(jié)果 | ||||
origin | |||||
lng | 起點經(jīng)度 | ||||
lat | 起點緯度 | ||||
destination | |||||
lng | 終點經(jīng)度 | ||||
lat | 終點緯度 | ||||
routes | 返回的方案集 | ||||
distance | 方案距離,單位:米 | ||||
duration | 線路耗時,單位:秒 | ||||
steps | 路線分段 | ||||
direction | 進(jìn)入道路的角度 | 枚舉值,返回值在0-11之間的一個值,共12個枚舉值,以30度遞進(jìn),即每個值代表角度范圍為30度;其中返回"0"代表345度到15度,以此類推,返回"11"代表315度到345度";分別代表的含義是:0-[345°-15°];1-[15°-45°];2-[45°-75°];3-[75°-105°];4-[105°-135°];5-[135°-165°];6-[165°-195°];7-[195°-225°];8-[225°-255°];9-[255°-285°];10-[285°-315°];11-[315°-345°] | |||
turn_type | 行駛轉(zhuǎn)向方向 | 如“直行”、“左前方轉(zhuǎn)彎” | |||
distance | 路段距離 | 單位:米 | |||
duration | 路段耗時 | 單位:秒 | |||
name | 道路名稱 | 如:“信息路” 若道路未明或百度地圖未采集到該道路名稱,則返回“無名路“ |
|||
instruction | 路段描述 | ||||
start_location | |||||
lng | 分段起點經(jīng)度 | ||||
lat | 分段起點緯度 | ||||
end_location | |||||
lng | 分段終點經(jīng)度 | ||||
lat | 分段終點緯度 | ||||
path | 分段坐標(biāo) |
4.4、示例代碼(支持多種語言)
文章來源:http://www.zghlxwxcb.cn/news/detail-494438.html
總之,根據(jù)自己的業(yè)務(wù)需求去選擇地圖的種類,以上只是介紹服務(wù)端,?感興趣的可以去試一下。文章來源地址http://www.zghlxwxcb.cn/news/detail-494438.html
到了這里,關(guān)于如何調(diào)用百度地圖API的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!