前言

华为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" />

代码开发关键步骤

  1. 初始化map,用于门路布局后果的展现
private MapFragment mMapFragment;private HuaweiMap hMap;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_directions);    mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment_mapfragmentdemo);    mMapFragment.getMapAsync(this);}
  1. 获取用户以后地位,作为门路布局的终点
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"));}
  1. 增加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;});

  1. 依据终点和起点信息,生成门路布局申请
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;}
  1. 依据门路布局响应,在地图上绘制门路布局后果
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/h...

欲了解更多详情,请参阅:

华为开发者联盟官网: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/Huaw...

下载demo和示例代码请到Github:https://github.com/HMS-Core

解决集成问题请到Stack Overflow:https://stackoverflow.com/que...


原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0204403880529210184?fid=18

原作者:胡椒