简体   繁体   中英

Google Maps polyline not drawing correctly

I'm making an app that when clicking on a marker it will open a dialog with a "Go" button, when the user press that button it show a path. I'm creating the path with the overview-polyline from google directions api, the problem is that the line is not correctly drawn, the line creates straight lines which makes the path pass over buildings like in the image:

在此处输入图片说明

How can I fix this?

I think you need to enable the Google Maps Direction API form the Google Developer Console.That should give you the path along the streets.

  • Go to Google Developer Console.
  • Click on Library.
  • Click Google Maps Directions API under Google Maps API.
  • Click on ENABLE button.

You can use google maps service to get the routes.

use the dependency for maps service and util

compile 'com.google.maps:google-maps-services:0.1.20'
compile 'com.google.maps.android:android-maps-utils:0.5+'

Create GeoApiContext object

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

Create a request for the map service using DirectionResult object. wait() method calls syncronously to the maps service.

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

Handle the response DirectionResult

//You can add markers to the start and end address like this

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

//Adding polyline like this.

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

Please refer to this. https://android.jlelse.eu/google-maps-directions-api-5b2e11dee9b0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM