Google Maps v3 Move Polygon

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 truesee 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.

google-maps-move-polygon

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() →

Did this help you out? Like what you see?
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!

☕️ Buy me a Coffee ($3)

(*) 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

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Join the Conversation

11 Comments

  1. This is exactly what I needed – good timing posting it! Thank you very much!

  2. 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.

    1. 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.

  3. 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

      1. 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

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.