MapBox使用之路线规划
生活随笔
收集整理的這篇文章主要介紹了
MapBox使用之路线规划
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Mapbox的初始化等操作此篇文章不贅述,直接忽略,直接用代碼說明怎么使用MapBox的路線規(guī)劃
1.導(dǎo)入
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:6.3.0' implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.41.0'目前MapBox只提供以下四種類型的路線規(guī)劃
2.大概流程就是通過傳經(jīng)緯度和路線類型請求數(shù)據(jù),然后根據(jù)請求回來的數(shù)據(jù)進(jìn)行畫線,以下完整代碼是根據(jù)“步行”和“駕車”兩種方法規(guī)劃出不同的路線,并且繪制出不同顏色。
mapView.getMapAsync(new OnMapReadyCallback() {@Overridepublic void onMapReady(MapboxMap mapboxMap) {map = mapboxMap;LatLng latLng = new LatLng(“起點緯度”,"起點經(jīng)度");MarkerOptions options = new MarkerOptions();Icon icon = IconFactory.getInstance(MapActivity.this).fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.map_icon_school));options.setIcon(icon);options.setPosition(latLng);mapboxMap.addMarker(options);mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));mapboxMap.addMarker(new MarkerOptions().icon(IconFactory.getInstance(MapActivity.this).fromResource(R.mipmap.map_icon_home)).position(new LatLng(“終點緯度”,"終點經(jīng)度");//特別注意,這里是經(jīng)度在前,緯度在后getRoute(Point.fromLngLat(“起點經(jīng)度”, “起點緯度”),Point.fromLngLat(“終點經(jīng)度”, “終點緯度”));}});private void getRoute(Point origin, Point destination) throws ServicesException {for (int i = 0; i < 2; i++) {String profile = "";switch (i) {case 0:profile = DirectionsCriteria.PROFILE_WALKING;break;case 1:profile = DirectionsCriteria.PROFILE_DRIVING;break;}MapboxDirections client = MapboxDirections.builder().origin(origin).destination(destination).profile(profile).destination(destination).accessToken(getString(R.string.mapbox_access_token)).build();String finalProfile = profile;client.enqueueCall(new Callback<DirectionsResponse>() {@Overridepublic void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {if (response.body() == null) {return;}// Print some info about the routeDirectionsRoute currentRoute = response.body().routes().get(0);// Draw the route on the mapdrawRoute(currentRoute, finalProfile);}@Overridepublic void onFailure(Call<DirectionsResponse> call, Throwable t) {Toast.makeText(MapActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();}});}}private void drawRoute(DirectionsRoute route, String profile) {// Convert LineString coordinates into LatLng[]LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_5);List<Point> coordinates = lineString.coordinates();LatLng[] points = new LatLng[coordinates.size()];for (int i = 0; i < coordinates.size(); i++) {points[i] = new LatLng(coordinates.get(i).latitude() / 10,coordinates.get(i).longitude()/ 10);}String color = "#30d176";switch (profile) {case DirectionsCriteria.PROFILE_WALKING:color = "#30d176";break;case DirectionsCriteria.PROFILE_DRIVING:color = "#48bef3";break;}// Draw Points on MapViewmap.addPolyline(new PolylineOptions().add(points).color(Color.parseColor(color)).width(5));}?
總結(jié)
以上是生活随笔為你收集整理的MapBox使用之路线规划的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 指纹识别技术相比于其它生物识别技术,有哪
- 下一篇: ajax中xhr监听,在firefox插