Google地图 - 沿直线折线获得X%的拉伸 [英] Google maps - get latlng of X% along a straight polyline

查看:164
本文介绍了Google地图 - 沿直线折线获得X%的拉伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



最接近的我已经来到了这个位置,我需要得到X百分比位置的纬度/经度沿着另外两个纬度/经度点之间的直线折线段。到目前为止使用了以下内容(对于该线的40%):

  google.maps.geometry.spherical.interpolate(startLatLng ,endLatLng,0.4); 

这适用于短距离,但对于长折线段,返回的latlng不在polyline旅行,因为它正在计算沿地球表面的最短距离,而不是通过平面地图上的最短距离。



任何帮助都会被赞赏, m只是缺少明显的东西。

解决方案

必须有一些经过验证和测试的方法才能做到这一点,但这是一种方法您可以使用Google地图API完成此操作:


  1. 将纬度/经度坐标转换为像素
  2. 在像素平面内插

  3. 转换回lat / lng

这大多非常简单;一个麻烦就是弄清楚如何处理穿过180子午线或杆子的线。

以下是一些经过半测试的代码,可以为您提供一个开始的地方:

 函数mercatorInterpolate(map,latLngFrom,latLngTo,fraction){
//获得投影点
var projection = map.getProjection();
var pointFrom = projection.fromLatLngToPoint(latLngFrom);
var pointTo = projection.fromLatLngToPoint(latLngTo);
//调整穿过180子午线的线
if(Math.abs(pointTo.x - pointFrom.x)> 128){
if(pointTo.x> pointFrom。 x)
pointTo.x - = 256;
else
pointTo.x + = 256;
}
//计算
之间的点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);
//计划返回lat / lng
var latLngBetween = projection.fromPointToLatLng(pointBetween);
返回latLngBetween;
}

我不能100%确定处理穿过180经线,但它在我尝试过的几次快速测试中起作用。

I need to get the latitude/longitude of a position at X percentage along a straight polyline segment between two other lat/lng points.

The closest I have come so far is using the following (for 40% along the line):

google.maps.geometry.spherical.interpolate(startLatLng, endLatLng, 0.4);

This works perfectly for short distances, however for a long polyline segment the latlng returned is outside of where the polyline travels, since it's working out the shortest distance along the earth's surface instead of the shortest distance across a flat map.

Any help on this would be appreciated, hopefully I'm just missing something obvious.

解决方案

There must be some proven and tested method out there to do this, but here is a way you could do it with the Maps API:

  1. Convert the lat/lng coordinates to pixels
  2. Interpolate in the pixel plane
  3. Convert back to lat/lng

This is mostly pretty straightforward; the one hassle is figuring out how to deal with lines that cross the 180 meridian or a pole.

Here's some semi-tested code that may give you a place to start:

function mercatorInterpolate( map, latLngFrom, latLngTo, fraction ) {
    // Get projected points
    var projection = map.getProjection();
    var pointFrom = projection.fromLatLngToPoint( latLngFrom );
    var pointTo = projection.fromLatLngToPoint( latLngTo );
    // Adjust for lines that cross the 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 back to lat/lng
    var latLngBetween = projection.fromPointToLatLng( pointBetween );
    return latLngBetween;
}

I'm not 100% sure about the part that handles lines that cross the 180 meridian, but it works OK in the few quick tests I tried.

这篇关于Google地图 - 沿直线折线获得X%的拉伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆