0

Both of these markers were placed on a Google map by handling the right click method on a polyline, ie:

google.maps.event.addListener(polyline, 'rightclick', myMethodThatHandlesIt);

The problem is that I was trying to locate the point (the first one up top) via C# spatial Geography.Intersects() and, well, it doesn't intersect.

It's completely dependent upon the zoom it seems. The top one, which again was created by right clicking on the (zoomed out) polyline, is over 1km away from the actually polyline.

Is there any way to ensure that the marker is placed ON the line, as in between the two line ends?

I'm sad google

Eric
  • 2,273
  • 2
  • 29
  • 44
  • [thread from the Google Maps API v3 group, Add point on polyline beetwen two existing points on click polyline event](https://groups.google.com/forum/#!topic/google-maps-js-api-v3/NeDvkj29xPI), might be useful – geocodezip Oct 04 '13 at 06:27

2 Answers2

1

Because it provides a better user experience, the hit box for markers, lines and shapes is somewhat larger than the actual item. So if you click close, it counts. That doesn't help with precision.

For polylines, you'd need to implement a snap-to-line functionality to find the closes point on the actual polyline. This answer provides some guidance: Find a point in a polyline which is closest to a latlng

Community
  • 1
  • 1
Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • Chad, I hear you on the better user experience but it seems like the event argument could at least include two properties then: one being the mouse location and another being the on-line location since that's got to be a significant rationale for clicking on a line and not just the map. – Eric Oct 05 '13 at 17:46
  • @radpin that sounds like an excellent idea. You should make a feature request here: https://code.google.com/p/gmaps-api-issues/issues/list?q=apitype:Javascript3 – Chad Killingsworth Oct 05 '13 at 17:59
  • solid idea, feature request #5887 created and submitted – Eric Oct 05 '13 at 18:08
0

For anyone else who runs into this, I couldn't figure out a clean way of doing it in Javascript and ultimately I didn't need to. The maker can be a little off but I need to persist it as a point that intersects the line. So the following method is in place to change the marker location before I store it.

I opted to use Microsoft.SqlServer.Types.SqlGeography which has a method of

Geography.ShortestLineTo(OtherGeography)

That gives a new line with two points, one at the user's marker location, the other at the closest intersection to the route/polyline/linestring.

The end of the new line is the closest point on the route to where the user right clicked on the polyline.

        public static System.Data.Spatial.DbGeography GetClosestPointOnRoute(System.Data.Spatial.DbGeography line, System.Data.Spatial.DbGeography point)
    {
        SqlGeography sqlLine = SqlGeography.Parse(line.AsText()).MakeValid(); //the linestring
        SqlGeography sqlPoint = SqlGeography.Parse(point.AsText()).MakeValid(); //the point i want on the line

        SqlGeography shortestLine = sqlPoint.ShortestLineTo(sqlLine); //find the shortest line from the linestring to point

        //lines have a start, and an end
        SqlGeography start = shortestLine.STStartPoint();
        SqlGeography end = shortestLine.STEndPoint();

        DbGeography newGeography = DbGeography.FromText(end.ToString(), 4326);

        var distance = newGeography.Distance(line);

        return newGeography;
    }

Additionally Issue 5887 was created over in Googleland to hopefully address.

Eric
  • 2,273
  • 2
  • 29
  • 44