Polylines with start and end of location in Google maps v3

空扰寡人 提交于 2019-12-24 17:24:18

问题


I have drawing polylines as the following:

var data = JSON.parse(data);
     var LineCordinates = new Array();
     for (i=0; i<data.length; i++){
     LineCordinates[i] = new google.maps.LatLng(data[i].fields.latitude, data[i].fields.longitude);
         }
      linePath = new google.maps.Polyline({
        path: LineCordinates,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
      });

      linePath.setMap(map);

    }

I want to place a marker at the start of the polygon line and end of the polygon line. How to do that?


回答1:


  • documentation on google.maps.Polyline
  • markers

    var startMarker = new google.maps.Marker({
          position:linepath.getPath().getAt(0), 
          map:map
        });
    var endMarker =  new google.maps.Marker({
          position:linepath.getPath().getAt(linepath.getPath().getLength()-1), 
          map:map
        });
    



回答2:


Since you hold coordinates of polyline in LineCordinates, you can use its first and last elements as a reference for your markers:

...
linePath.setMap(map);

var marker1 = new google.maps.Marker({
   position: LineCordinates[0],
   map: map,
   title: "Start"
});

var marker2 = new google.maps.Marker({
   position: LineCordinates[ LineCordinates.length - 1 ],
   map: map,
   title: "End"
});
...


来源:https://stackoverflow.com/questions/23198372/polylines-with-start-and-end-of-location-in-google-maps-v3

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