hiding the last direction marker with Google maps api

梦想与她 提交于 2019-12-10 19:08:30

问题


I've calculated a route with the same begin and end point with the maps api.

Because the begin and end point are the same, the first marker is overlapped by the last marker. Now I want only the last marker removed.

I only know how to hide them all with:

directionsDisplay.suppressMarkers = true;

Is there a way to loop through the markers and remove the last one?

this is the code I use for the directions:

function calcRoute(waypts) {
    var start = waypts[0].location;
    var end = waypts[0].location;


       var request = {
        origin:start,
        destination:end,
        waypoints:waypts,
        optimizeWaypoints: true,
        provideRouteAlternatives:false,
        travelMode:google.maps.TravelMode.DRIVING
       };

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {

           directionsDisplay.suppressInfoWindows = true;
           directionsDisplay.suppressMarkers = true;
           directionsDisplay.setDirections(response);

           console.log(status);

    }else{
         alert('SATUS:'+response.status);

    }
 });
}

回答1:


Try this

In initialise

directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});

then

directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var route = response.routes[0];
    var start =route.legs[0].start_location;
    var end =route.legs[0].end_location;
    addMarker(end);

addMarker function

    function addMarker(pos){
var marker = new google.maps.Marker({
  position: pos, 
  map: map, 
  icon: 'images/your image'

}
)
}

I have not tested this but you should get the idea



来源:https://stackoverflow.com/questions/9635805/hiding-the-last-direction-marker-with-google-maps-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!