javascript - Google maps - get latlng of X% along a straight polyline -


i need latitude/longitude of position @ x percentage along straight polyline segment between 2 other lat/lng points.

the closest have come far using following (for 40% along line):

google.maps.geometry.spherical.interpolate(startlatlng, endlatlng, 0.4); 

this works short distances, long polyline segment latlng returned outside of polyline travels, since it's working out shortest distance along earth's surface instead of shortest distance across flat map.

any on appreciated, i'm missing obvious.

there must proven , tested method out there this, here way maps api:

  1. convert lat/lng coordinates pixels
  2. interpolate in pixel plane
  3. convert lat/lng

this pretty straightforward; 1 hassle figuring out how deal lines cross 180 meridian or pole.

here's semi-tested code may give place start:

function mercatorinterpolate( map, latlngfrom, latlngto, fraction ) {     // projected points     var projection = map.getprojection();     var pointfrom = projection.fromlatlngtopoint( latlngfrom );     var pointto = projection.fromlatlngtopoint( latlngto );     // adjust lines cross 180 meridian     if( math.abs( pointto.x - pointfrom.x ) > 128 ) {         if( pointto.x > pointfrom.x )             pointto.x -= 256;         else             pointto.x += 256;     }     // calculate point between     var x = pointfrom.x + ( pointto.x - pointfrom.x ) * fraction;     var y = pointfrom.y + ( pointto.y - pointfrom.y ) * fraction;     var pointbetween = new google.maps.point( x, y );     // project lat/lng     var latlngbetween = projection.frompointtolatlng( pointbetween );     return latlngbetween; } 

i'm not 100% sure part handles lines cross 180 meridian, works ok in few quick tests tried.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -