猿问

Android Studio 中的说明

我正在尝试研究如何在 android studio(谷歌地图)上获取从用户位置到选定标记的方向。你们有什么想法吗?


泛舟湖上清波郎朗
浏览 138回答 2
2回答

喵喵时光机

使用谷歌地图距离矩阵 api 通过给定代码查找从源到目的地的路径DateTime now = new DateTime();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DirectionsResult result = DirectionsApi.newRequest(getGeoContext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mode(TravelMode.DRIVING)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .origin(new com.google.maps.model.LatLng(fromLat, fromLng))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .destination(new com.google.maps.model.LatLng(ToLat, ToLng))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .departureTime(now)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .await();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addMarkersToMap(result, googleMap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addPolyline(result, googleMap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (loader!=null && loader.isShowing()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader.dismiss();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (ApiException ignored) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("Exception"," "+ignored.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException | IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("Exception"," InterruptedException "+e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }添加标记private void addMarkersToMap(DirectionsResult results, GoogleMap mMap) {&nbsp; &nbsp; &nbsp; &nbsp; mMap.addMarker(new MarkerOptions().position(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new LatLng(results.routes[0].legs[0].startLocation.lat,results.routes[0].legs[0].startLocation.lng))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .title(from)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));&nbsp; &nbsp; &nbsp; &nbsp; mMap.addMarker(new MarkerOptions().position(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new LatLng(results.routes[0].legs[0].endLocation.lat,results.routes[0].legs[0].endLocation.lng))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .title(to)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));&nbsp; &nbsp; }绘制折线并超时以从谷歌的服务器获取数据private GeoApiContext getGeoContext() {&nbsp; &nbsp; &nbsp; &nbsp; GeoApiContext geoApiContext = new GeoApiContext();&nbsp; &nbsp; &nbsp; &nbsp; return geoApiContext.setQueryRateLimit(3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setApiKey(getString(R.string.directionsApiKey))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setConnectTimeout(1, TimeUnit.SECONDS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setReadTimeout(1, TimeUnit.SECONDS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setWriteTimeout(1, TimeUnit.SECONDS);&nbsp; &nbsp; }private void addPolyline(DirectionsResult results, GoogleMap mMap) {&nbsp; &nbsp; &nbsp; &nbsp; List<LatLng> decodedPath = PolyUtil.decode(results.routes[0].overviewPolyline.getEncodedPath());&nbsp; &nbsp; &nbsp; &nbsp; int PATTERN_DASH_LENGTH_PX = 20;&nbsp; &nbsp; &nbsp; &nbsp; int PATTERN_GAP_LENGTH_PX = 20;&nbsp; &nbsp; &nbsp; &nbsp; PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);&nbsp; &nbsp; &nbsp; &nbsp; PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);&nbsp; &nbsp; &nbsp; &nbsp; List<PatternItem> PATTERN_POLYGON_ALPHA = Arrays.asList(GAP, DASH);&nbsp; &nbsp; &nbsp; &nbsp; mMap.addPolyline(new PolylineOptions()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .geodesic(true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .color(getResources().getColor(R.color.colorPrimary))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .width(5)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .pattern(PATTERN_POLYGON_ALPHA)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addAll(decodedPath));&nbsp; &nbsp; }

繁星coding

1.使用谷歌地图方向api获取方向latlng。谷歌方向api2.之后,您需要使用折线在地图上绘制路径。这是完整的教程链接
随时随地看视频慕课网APP

相关分类

Java
我要回答