日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MapBox使用之路线规划

發(fā)布時(shí)間:2023/12/18 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MapBox使用之路线规划 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)緯度和路線類型請(qǐng)求數(shù)據(jù),然后根據(jù)請(qǐng)求回來的數(shù)據(jù)進(jìn)行畫線,以下完整代碼是根據(jù)“步行”和“駕車”兩種方法規(guī)劃出不同的路線,并且繪制出不同顏色。

mapView.getMapAsync(new OnMapReadyCallback() {@Overridepublic void onMapReady(MapboxMap mapboxMap) {map = mapboxMap;LatLng latLng = new LatLng(“起點(diǎn)緯度”,"起點(diǎn)經(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(“終點(diǎn)緯度”,"終點(diǎn)經(jīng)度");//特別注意,這里是經(jīng)度在前,緯度在后getRoute(Point.fromLngLat(“起點(diǎn)經(jīng)度”, “起點(diǎn)緯度”),Point.fromLngLat(“終點(diǎn)經(jīng)度”, “終點(diǎn)緯度”));}});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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。