UPDATE 2018.03.07: The issue requesting to have this in the core of Google Maps got marked as fixed … unfortunately I cannot find this change back in the documentation, so I’m not too sure if it’s actually fixed or not. Will keep tracking it.
Since Google Maps v3.11 (*) it’s possible to move around a google.maps.Polygon
by using the mouse (if the draggable
property is set to true
— see example here). Unfortunately the API does not allow us to move that Polygon programatically.
google.maps.Polygon.moveTo()
is a Google Maps extension enabling just that: it allows one to programmatically move a google.maps.Polygon
on the map without any user/mouse interaction. The script works with both geodesic (polygons that follow the curvature of the earth) and non-geodesic (straight) polygons.
In the screenshot above, the red polygon is the original one, and the green one is the one that’s been moved using google.maps.Polygon.moveTo()
. The distortion is normal, due to the Mercator Projection
google.maps.Polygon.moveTo() →
Consider donating.
I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!
(*) The current stable version of Google Maps still is 3.10. For now (until 3.11 is the stable one), you can include the experimental 3.11 version by referencing http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false
This is exactly what I needed – good timing posting it! Thank you very much!
Heads up — since this script replaces the paths you’ll need to re-add any path events you have. I’ll work on a pull request for this at some point.
Hi Collin,
That indeed is the case (didn’t take it into account when developing the plugin). Looking forward to the pull request.
Regards,
Bram.
I need to move several polygon, you method is exactly what I’m looking for!
You did it 5 years ago, is it still the right way to do it?
thanks,
Cyril
Yes. The Polygon class in the current JS Google Maps release (3.30 at the time of writing) still has no built-in support for
.moveTo()
.Thanks for you quick answer and for this great job !
Hello,
In my case, the polygons are loaded from a GeoJson file: map.data.loadGeoJson(‘https……
How can I use the moveTo function in this case?
Thanks,
Cyril
My GeoJson file contains MultiPolygon, is it working in this case?
Thanks,
Cyril
omg, you are the man.. was looking for exact same solution…. Thank you so very much…