javascript 如何计算折线距离?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15796372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:08:34  来源:igfitidea点击:

how to calculate polyline distance?

javascriptgoogle-mapsgoogle-maps-api-3

提问by user2232995

function createLine()
{

geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(7.5653, 80.4303);
var mapOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("directionpanel"), mapOptions);

var address = document.getElementById('startvalue').value;
var address2 = document.getElementById('endvalue').value;

var temp, temp2;

geocoder.geocode({ 'address': address }, function (results, status) {
    temp = results[0].geometry.location;
    geocoder.geocode({ 'address': address2 }, function (results, status) {
        temp2 = results[0].geometry.location;

    var route = [
      temp,
      temp2
    ];

    var polyline = new google.maps.Polyline({
        path: route,
        strokeColor: "#ff0000",
        strokeOpacity: 0.6,
        strokeWeight: 5
    });

    polyline.setMap(map);
    });
});
}

This code work fine. I want to calculate the distance of the line which I created. I want to display this value in meters via an alert. Please help me... (In this case two points connect directly with the line. I want to calculate that direct line distance. )

这段代码工作正常。我想计算我创建的线的距离。我想通过警报以米为单位显示此值。请帮帮我...(在这种情况下,两点直接与直线相连。我想计算直线距离。)

回答by geocodezip

Use google.maps.geometry.spherical.computeLength(be sure to include the geometry library)

使用google.maps.geometry.sphereal.computeLength(确保包含几何库

function createLine()
{

geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(7.5653, 80.4303);
var mapOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("directionpanel"), mapOptions);

var address = document.getElementById('startvalue').value;
var address2 = document.getElementById('endvalue').value;

var temp, temp2;

geocoder.geocode({ 'address': address }, function (results, status) {
    temp = results[0].geometry.location;
    geocoder.geocode({ 'address': address2 }, function (results, status) {
        temp2 = results[0].geometry.location;

    var route = [
      temp,
      temp2
    ];

    var polyline = new google.maps.Polyline({
        path: route,
        strokeColor: "#ff0000",
        strokeOpacity: 0.6,
        strokeWeight: 5
    });

    var lengthInMeters = google.maps.geometry.spherical.computeLength(polyline.getPath());
    alert("polyline is "+lengthInMeters+" long");

    polyline.setMap(map);
    });
});
}

Working example(updated the example from your last question to include the length calculation)

工作示例(更新了您上一个问题中的示例以包括长度计算)