Java 谷歌地图多段线绘制不正确
我正在制作一个应用程序,当点击一个标记时,它会打开一个带有“Go”按钮的对话框,当用户按下该按钮时,它会显示一个路径。我正在使用google directions api中的概览多段线创建路径,问题是该线绘制不正确,该线创建的直线使路径穿过建筑物,如图所示:Java 谷歌地图多段线绘制不正确,java,android,google-maps,google-polyline,Java,Android,Google Maps,Google Polyline,我正在制作一个应用程序,当点击一个标记时,它会打开一个带有“Go”按钮的对话框,当用户按下该按钮时,它会显示一个路径。我正在使用google directions api中的概览多段线创建路径,问题是该线绘制不正确,该线创建的直线使路径穿过建筑物,如图所示: 如何解决此问题?我认为您需要从Google开发者控制台启用Google Maps Direction API。这将为您提供道路 转到谷歌开发者控制台 点击图书馆 单击谷歌地图API下的谷歌地图方向API 点击启用按钮 您可以使用谷歌地图服
如何解决此问题?我认为您需要从Google开发者控制台启用Google Maps Direction API。这将为您提供道路
- 转到谷歌开发者控制台
- 点击图书馆
- 单击谷歌地图API下的谷歌地图方向API
- 点击启用按钮
compile 'com.google.maps:google-maps-services:0.1.20'
compile 'com.google.maps.android:android-maps-utils:0.5+'
创建GeoApiContext对象
private GeoApiContext createGeoApiContext() {
GeoApiContext geoApiContext = new GeoApiContext();
return geoApiContext.setQueryRateLimit(5)
.setApiKey(GOOGLE_API_KEY)
.setConnectTimeout(5, TimeUnit.SECONDS)
.setReadTimeout(5, TimeUnit.SECONDS)
.setWriteTimeout(5, TimeUnit.SECONDS);
}
使用DirectionResult对象为映射服务创建请求。
wait()方法同步调用映射服务
DateTime now = new DateTime();
try {
DirectionsResult result = DirectionsApi.newRequest(createGeoApiContext())
.mode(TravelMode.DRIVING)
.origin(new com.google.maps.model.LatLng(13.3392, 77.1140))
.destination(new com.google.maps.model.LatLng(13.9299, 75.5681))
.alternatives(true)
.departureTime(now).await();
addMarkersToMap(result,map);
} catch (ApiException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
处理响应DirectionResult
//您可以像这样向起始地址和结束地址添加标记
private void addMarkersToMap(DirectionsResult results, GoogleMap mMap) {
for (int i=0;i<results.routes.length;i++) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(results.routes[i].legs[0].startLocation.lat,
results.routes[i].legs[0].startLocation.lng)).
title(results.routes[i].legs[0].startAddress));
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(results.routes[i].legs[0].endLocation.lat,
results.routes[i].legs[0].endLocation.lng)).
title(results.routes[i].legs[0].endAddress).snippet(getEndLocationTitle(results,i)));
addPolyline(results, map, i);
}
}
private void addMarkersToMap(directions结果,GoogleMap mMap){
对于(int i=0;我看一下
private String getEndLocationTitle(DirectionsResult results,int i) {
return "Time :" + results.routes[i].legs[0].duration.humanReadable +
" Distance :" + results.routes[i].legs[0].distance.humanReadable;
}
private void addPolyline(DirectionsResult results, GoogleMap mMap,int i) {
List<LatLng> decodedPath = PolyUtil.decode(results.routes[i].overviewPolyline.getEncodedPath());
mMap.addPolyline(new PolylineOptions().addAll(decodedPath).width(5));
}
}