place marker on polyline with specific distance

前端 未结 2 589
情深已故
情深已故 2021-01-16 01:00

I have made a google map using google map api v3 and placed a polyline on map here is my code for my map

function initialize() {
            var myLatLng = n         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-16 01:12

    This works for me (added to the end of your initialize function):

    var i=1;
    var length = google.maps.geometry.spherical.computeLength(flightPath.getPath());
    var remainingDist = length;
    
    while (remainingDist > 0)
    {
       createMarker(map, flightPath.GetPointAtDistance(1000*i),i+" km");
       remainingDist -= 1000;
       i++;
    }
    // put markers at the ends
    createMarker(map,flightPath.getPath().getAt(0),length/1000+" km");
    createMarker(map,flightPath.getPath().getAt(flightPath.getPath().getLength()-1),(length/1000).toFixed(2)+" km");
    flightPath.setMap(map);
    }
    
    function createMarker(map, latlng, title){
        var marker = new google.maps.Marker({
              position:latlng,
              map:map,
              title: title
              });
    }
    

    example

提交回复
热议问题