3

Does anybody knows how to make a Marker or a Polyline snap into the coordinates of a existing Polyline?

I'm looking for something like the behavior in the googlemaps engine lite: https://mapsengine.google.com

If you select a Polyline or Marker there and try to edit another polyline coordinate (using ctrl or shift) it will snap into the marker or the polyline coordinates

Kara
  • 6,115
  • 16
  • 50
  • 57
  • 5
    [Confine dragging of Google Maps V3 Marker to Polyline](http://stackoverflow.com/questions/10694378/confine-dragging-of-google-maps-v3-marker-to-polyline) – geocodezip Aug 20 '13 at 05:52

1 Answers1

0

There is, as far as I know, no easy way to do it. Polylines only have the locations (latLng objects) that you initially pass them and that is it.

So, with this in mind, you can take two approaches:

  1. Instead of a Polyline, you can draw a Polygon.
  2. Change your Polyline to include more points.

Polygon

With this approach, you would have to draw a very thin polygon, thin enough to look like a line.

Wit this approach you can easily check if the marker is inside the polygon by using the containsLocation() method, and if not, set its new position to inside the Polygon.

The drawback is that your Polygon needs to be very, very thin, and you need to set a width for the line. If the width is too big, the dragging will look inaccurate, and if it is too small you may miss it.

Polyline

With this approach, you would have to add multiple points to the polyline, and then move the marker to one of those points every time it went out.

This way there is no need to create a width to calculate the line, but you need to calculate dozens if not hundreds of extra points automatically, and then add them to the Polyline.

Both solutions would work, and both have pros and cons. In the end, it pretty much goes around the which poison do you prefer old saying.

Extra

Theory aside, I did find a good example for the Polylines strategy (kudos to @geocodezip for the comment and @BradBarrow for the response).

Community
  • 1
  • 1
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266