Editable lines in Google Maps using the Javascript v3 API

The Google Maps Javascript API v3 provides overlays for drawing shapes, such as Polygons, Circles, Polylines and Rectangles. The shapes are not implicitly interactive in the respect that you cannot tell the shape to be ‘editable’ as you could in the Javascript v2 API (here’s an example).

To simulate the behavior using functionality provided by maps v3 API, you’ll need to find an element that supports the drag event, such as the Marker object. A marker has a position, a shape and provides events such as click, dragstart, drag and dragend.

The code

First off, we need to load the google maps API and specify which version to use. The code uses some extra goodies to calculate the length of the line, which is provided in the geometry library and it looks like this is available in the v3.3 of the API. Here’s how to do specify this:

The complete code looks like:

To start drawing, click on the map to specify the starting point and move the mouse to the end point.

The highlighted function is the most interesting. It creates a marker representing one segment of the path of a PolyLine. The marker knows about the previous and next marker and calculates its position in the MVCArray of the path so that it updates the geo-position of the correct segment. This is done by the getPathIndex() function added to the marker.

The Marker.dragStart() function checks whether the marker is the first or last marker in the path. If it is, a segment is added to the PolyLine at the geo-position where the drag starts, and a new marker is created representing the new segment. This means that the line is extended when dragging the end markers.

The Marker.beingDragged() function updates the position of the segment for the PolyLine to the geo-position for the marker, which makes the segment ‘editable’.

When clicking on a marker, the length of the line is computed using the google.maps.geometry.spherical.computeLength() function and an InfoWindow is shown displaying the length and the longitude/latitude for the selected marker.

Conclusion

It is possible to simulate the older Google Maps Javascript V2 functionality for editing shapes but it requires you to write your own editing functionality. The code above is just a proof-of-concept and needs a lot more love and caring to be useful.

This Post Has 6 Comments

  1. It seems Google has updated the V3 API. You can now set the property “editable” in a polygon or polyline making them user editable. However, I am still trying to figure out how to delete a point… (as a user, not in code).

  2. Perfect, thanks.

  3. Hi Tobais,

    I am trying to edit a selected polygon to edit from all loaded polygons so that the selected polygon can be updated.

    can you help me

    thanks
    Naveen

  4. thaaaaaaaaaaaaaaaaaaaaaaaaaanks :) :D

    @naveen :
    you can do that by setting the option editable:true for that polygon

  5. perfect.thank you so much.

Leave a Reply

Close Menu