【问题标题】:Google Maps polyline not drawing correctly谷歌地图折线未正确绘制
【发布时间】:2017-03-30 15:14:10
【问题描述】:

我正在制作一个应用程序,当单击标记时,它将打开一个带有“Go”按钮的对话框,当用户按下该按钮时,它会显示一条路径。我正在使用来自谷歌方向 api 的概述折线创建路径,问题是该线未正确绘制,该线创建直线,使路径通过图像中的建筑物:

我该如何解决这个问题?

【问题讨论】:

标签: java android google-maps google-polyline


【解决方案1】:

我认为您需要从 Google 开发者控制台启用 Google Maps Direction API。这应该会为您提供街道上的路径。

  • 转到 Google 开发者控制台。
  • 点击图书馆。
  • 点击 Google Maps API 下的 Google Maps Directions API。
  • 点击启用按钮。

【讨论】:

    【解决方案2】:

    您可以使用谷歌地图服务获取路线。

    使用地图服务和实用程序的依赖项

    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 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));
        }
    }
    

    请参考这里。 https://android.jlelse.eu/google-maps-directions-api-5b2e11dee9b0

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2013-07-02
      • 2023-04-06
      • 1970-01-01
      • 2018-03-06
      相关资源
      最近更新 更多