前言
華為Map Kit提供的路徑規划API是一套以HTTPS形式提供的步行、騎行、駕車路徑規划以及行駛距離計算接口,通過JSON格式返回路徑查詢數據,提供路徑規划能力。
路徑規划具體提供如下功能:
- 步行路徑規划 API提供100km以內的步行路徑規划能力。
- 騎行路徑規划 API提供100km以內的騎行路徑規划能力。
- 駕車路徑規划 API提供駕車路徑規划能力,支持以下功能:
-支持一次請求返回多條路線,最多支持3條路線。
-最多支持5個途經點。
-支持未來出行規划。
-支持根據實時路況進行合理路線規划。
場景
用車服務:利用即時和未來出行路線規划為訂單提供准確的價格預估。在派單場景中,利高性能批量到達時間預估(ETA)服務,提升派單效率。
物流:利用駕車和騎行路線規划,為支干線物流和末端配送提供准確的路線規划、耗時預估和道路收費預測。
旅游:用戶在預定酒店、設計旅游線路時,通過路線規划分析酒店、景點、交通站點之間的路線距離,幫助用戶更高效規划行程。
開發前准備
l 路徑規划服務使用前,需要在開發者聯盟網站上獲取API KEY。
說明
如果API KEY包含特殊字符,則需要進行encodeURI編碼。例如:原始API KEY:ABC/DFG+ ,轉換結果: ABC%2FDFG%2B。
l 在AndroidManifest.xml文件里面申請訪問網絡權限
<uses-permission android:name="android.permission.INTERNET" />
代碼開發關鍵步驟
- 初始化map,用於路徑規划結果的展示
private MapFragment mMapFragment;
private HuaweiMap hMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_directions);
mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment_mapfragmentdemo);
mMapFragment.getMapAsync(this);
}
- 獲取用戶當前位置,作為路徑規划的起點
private void getMyLocation() {
Task<Location> locationTask = LocationServices.getFusedLocationProviderClient(this).getLastLocation();
locationTask.addOnCompleteListener(param0 -> {
if (param0 != null) {
Location location = param0.getResult();
double Lat = location.getLatitude();
double Lng = location.getLongitude();
myLocation = new LatLng(Lat, Lng);
Log.d(TAG, " Lat is : " + Lat + ", Lng is : " + Lng);
CameraUpdate CameraUpdate = CameraUpdateFactory.newLatLng(myLocation);
hMap.moveCamera(CameraUpdate);
}
}).addOnFailureListener(param0 -> Log.d(TAG, "lastLocation is error"));
}
- 添加map長按事件,用於響應用戶設定的路徑規划終點
hMap.setOnMapLongClickListener(latLng -> {
if (null != mDestinationMarker) {
mDestinationMarker.remove();
}
if (null != mPolylines) {
for (Polyline polyline : mPolylines) {
polyline.remove();
}
}
enableAllBtn();
MarkerOptions options = new MarkerOptions().position(latLng).title("dest");
mDestinationMarker = hMap.addMarker(options);
mDestinationMarker.setAnchor(0.5f,1f);
StringBuilder dest = new StringBuilder(String.format(Locale.getDefault(), "%.6f", latLng.latitude));
dest.append(", ").append(String.format(Locale.getDefault(), "%.6f", latLng.longitude));
((TextInputEditText)findViewById(R.id.dest_input)).setText(dest);
mDest = latLng;
});
- 根據起點和終點信息,生成路徑規划請求
private JSONObject buildRequest() {
JSONObject request = new JSONObject();
try {
JSONObject origin = new JSONObject();
origin.put("lng", myLocation.longitude);
origin.put("lat", myLocation.latitude);
JSONObject destination = new JSONObject();
destination.put("lng", mDest.longitude);
destination.put("lat", mDest.latitude);
request.put("origin", origin);
request.put("destination", destination);
} catch (JSONException e) {
e.printStackTrace();
}
return request;
}
- 根據路徑規划響應,在地圖上繪制路徑規划結果
JSONObject route = new JSONObject(result);
JSONArray routes = route.optJSONArray("routes");
JSONObject route1 = routes.optJSONObject(0);
JSONArray paths = route1.optJSONArray("paths");
JSONObject path1 = paths.optJSONObject(0);
JSONArray steps = path1.optJSONArray("steps");
for (int i = 0; i < steps.length(); i++) {
PolylineOptions options = new PolylineOptions();
JSONObject step = steps.optJSONObject(i);
JSONArray polyline = step.optJSONArray("polyline");
for (int j = 0; j < polyline.length(); j++) {
JSONObject polyline_t = polyline.optJSONObject(j);
options.add(new LatLng(polyline_t.getDouble("lat"), polyline_t.getDouble("lng")));
}
Polyline pl = hMap.addPolyline(options.color(Color.BLUE).width(3));
mPolylines.add(pl);
}
Demo效果
Github源碼
如果你對實現方式感興趣,可以查看Github上的源碼:https://github.com/HMS-Core/hms-mapkit-demo-java
欲了解更多詳情,請參閱:
華為開發者聯盟官網:https://developer.huawei.com/consumer/cn/hms?ha_source=hms1
獲取開發指導文檔:https://developer.huawei.com/consumer/cn/doc/development?ha_source=hms1
參與開發者討論請到Reddit社區:https://www.reddit.com/r/HuaweiDevelopers/
下載demo和示例代碼請到Github:https://github.com/HMS-Core
解決集成問題請到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest
原文鏈接:https://developer.huawei.com/consumer/cn/forum/topic/0204403880529210184?fid=18
原作者:胡椒