Google 地图 - 绘制多个纬度经度对之间的线条和距离(直线距离)

问题描述 投票:0回答:4

我希望通过多个纬度经度对在地图上“直线飞行”绘制一条路线。我还需要计算总距离(最好是点之间的距离)。

我找到了这个例子http://www.daftlogic.com/projects-advanced-google-maps-distance-calculator.htm但它比我需要的要复杂得多 - 我不需要任何类型的搜索功能,因为我已经有了我的纬度经度数据。然而,它显示路线的方式正是我想要实现的

是否有我可以借鉴的更简单的示例或实现此目标的任何一般指导?

google-maps dictionary google-maps-api-3
4个回答
6
投票

“如乌鸦飞翔”一样绘制路线很简单,只需将折线的测地线选项设置为 true 即可:

var geodesic = new google.maps.Polyline({geodesic: true});

您可以使用这个例子: http://code.google.com/apis/maps/documentation/javascript/examples/geometry-headings.html

要计算距离,您可以利用 Google Maps API V3 的几何库。 http://code.google.com/apis/maps/documentation/javascript/geometry.html

计算很简单:

var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngOfPointA, latLngOfPointB);

要计算路径的长度,您可以使用computeLength函数:

var path = geodesic.getPath();
var length = google.maps.geometry.spherical.computeLength(path)

2
投票

请务必在对 Google Maps API 的调用中包含几何库。

来自文档的引导

<script type="text/javascript"       src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=true_or_false">  </script>

0
投票

我有一个包含 Vincenty 公式的 Excel 文件,我可以转发。 20'000 公里精度优于 20 厘米。


0
投票

通过 Java Google Maps SDK,您可以获取特定地址的 lat/lng,然后使用半正矢距离公式获取以 KM 为单位的距离。

    <dependency>
        <groupId>com.google.maps</groupId>
        <artifactId>google-maps-services</artifactId>
        <version>2.2.0</version>
    </dependency>

代码:

public static Double getDistanceInKmAsTheCrowFlies(String apiKey, String address1, String address2)
{
    GeocodingResult[] address1GeoCode = getGeocoding(apiKey, address1);
    GeocodingResult[] address2GeoCode = getGeocoding(apiKey, address2);

    if (address1GeoCode != null && address2GeoCode != null)
    {
        double lat1 = address1GeoCode[0].geometry.location.lat;
        double lng1 = address1GeoCode[0].geometry.location.lng;

        double lat2 = address2GeoCode[0].geometry.location.lat;
        double lng2 = address2GeoCode[0].geometry.location.lng;

        return getHaversineDistance(lat1, lng1, lat2, lng2);
    }
    return null;

}

public static GeocodingResult[] getGeocoding(String apiKey, String address)
{

    GeocodingApiRequest req = GeocodingApi.newRequest(new GeoApiContext.Builder()
            .apiKey(apiKey)
            .build());

    GeocodingResult[] query = req.address(address).awaitIgnoreError();

    if (query != null && query.length > 0)
    {
        return query;
    }
    return null;
}

public static double getHaversineDistance(double lat1, double lng1, double lat2, double lng2)
{
    int r = 6371; // average radius of the earth in km
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
            * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = r * c;
    return d;
}
© www.soinside.com 2019 - 2024. All rights reserved.