1

I need to find coordinates (lat,lng) on the plotted polyline to show a tooltip there, other than the coordinates that i have used to plot the path. Can anyone pls help me out? :(

anusreemn
  • 1,047
  • 1
  • 10
  • 24
  • 1
    Good question. I too need a solution for the same question. – Nidhin S G Mar 16 '15 at 10:38
  • @NidhinSG Shaey, chumma :P – Nevin Madhukar K Mar 16 '15 at 10:39
  • What does your code currently look like? What coordinates? What have you tried? – geocodezip Mar 16 '15 at 11:17
  • @geocodezip I found my answer here : http://stackoverflow.com/questions/19128954/latlng-from-polyline-percentage..!! wow B-) – anusreemn Mar 16 '15 at 12:07
  • @NevinMadhukarK hehe odikko – Nidhin S G Mar 16 '15 at 12:42
  • @geocodezip I included the v3_deploy library and tried : var latlng = polyline.GetPointAtDistance(polyline.Distance()*(desired percentage)/100); It does return a (lat,lng) but the point is not necessarily on the plotted PolyLine. Any suggestions? – anusreemn Mar 16 '15 at 13:12
  • Please provide a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that exhibits the issue. – geocodezip Mar 16 '15 at 13:35
  • Really sorry for the delay. I was into building this fiddle : http://jsfiddle.net/E45Su/58/. Click on 'Get Latlng'. You can see that the infoWindow opens up at different location on the map rather than at a position on the polyline. – anusreemn Mar 16 '15 at 13:38
  • @user3513687 The tooltip appears borked because that answer has geodesic as `off`, not `on` (as in your case). Google Maps does not expose the path calculation of geodesics, so you have to reimplement it. I'll provide a quick and dirty patch to it in a tiny bit. – Sébastien Renauld Mar 16 '15 at 13:59

1 Answers1

1

The issue you're facing is that you are using the geodesic parameter, which forces the polyline to be drawn following the curvature of the Earth, yet, when you decide where to draw the tooltip, you decide to not follow and go for the straight point-to-point line and distance.

In order to avoid reinventing the wheel too much, I've scavenged this piece of code from another JS lib. jsfiddle would not allow me to use it as an external ref and it isn't on any CDN, so excuses for the direct copy-pasta. The alternative would've meant a full reimplementation of the solution to Vincenty's equation.

From there, it's a simple matter to rework your function to get the point as:

var getPoint = function(path, percentage) {
  var p1 = new LatLon(path.getAt(0).lat(), path.getAt(0).lng());
  var p2 = new LatLon(path.getAt(1).lat(), path.getAt(1).lng());
  var bearing = p1.bearingTo(p2);
  var d = p1.distanceTo(p2);
  var o = p1.destinationPoint(d * percentage, bearing);
  return new google.maps.LatLng(o.lat, o.lon);
};

The result: http://jsfiddle.net/E45Su/70/

Please note that this is heavily dependent on the google maps API as it expects an MVCArray as path input.

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66