开发者

google maps mousemove and click listener together

开发者 https://www.devze.com 2023-02-12 09:31 出处:网络
I use a mousemove listener on Google Maps, while I want handle the click event too. var path = new google.maps.MVCArray;

I use a mousemove listener on Google Maps, while I want handle the click event too.

var path = new google.maps.MVCArray;
...
line = new google.maps.Polyline({
  map: map,
  pat开发者_如何转开发h: new google.maps.MVCArray([path]),
});
google.maps.event.addListener(map, 'click', function(event) {
   path.push(event.latLng);
});
google.maps.event.addListener(map, 'mousemove', function(event) {
  if (path.getLength() > 1) path.setAt(path.getLength()-1,event.latLng);
});

I want to follow the mouse with the line, but if the user click on the map, push the polyline's array. But the click event doesn't work... Any idea?


I think there is a small bug in your code:

On line 5 it should read

path: path,

instead of

path: new google.maps.MVCArray([path]),

Reason: Your var path is already an MCVArray and the property path of PolylineOptions expects just an MVCArray, but you supply it with an MVCArray inside a normal array inside an MVCArray.

This prevents the code following from pushing the coordinates to the correct array.

0

精彩评论

暂无评论...
验证码 换一张
取 消