如何计算折线距离?

问题描述:

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); 
    }); 
}); 
} 

此代码的工作正常。我想计算我创建的线的距离。我不会通过alert.Meta显示多米线值,请帮助我...(在这种情况下两点连接直接line.i将不会计算直线距离。)如何计算折线距离?

+0

看看这个答案http://*.com/questions/1502590/calculate-distance-between-two-points-in-google-maps-v3 – hitautodestruct 2013-04-03 19:37:32

使用google.maps.geometry.spherical.computeLength(一定要包括geometry library)从

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(更新例子你最后一个问题包括长度c alculation)

+0

感谢您的帮助。 – user2232995 2013-04-04 08:33:07

+0

如果你认为这是一个重复的,你应该投票结束它,引用重复的帖子。虽然我不认为它是该特定帖子的副本,特别是因为该帖子早于几何图书馆,它增加了对计算多段线长度的本地支持。 – geocodezip 2013-04-03 20:02:09