Javascript 如何计算多段线距离?

Javascript 如何计算多段线距离?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,这段代码运行良好。我想计算我创建的直线的距离。我希望通过警报以米为单位显示此值。请帮帮我。。。(在这种情况下,两个点直接与直线连接。我想计算直线距离。)使用(确保包括) (更新了上一个问题的示例,以包括长度计算) 代码片段: var地理编码器; var映射; 函数初始化(){ geocoder=新的google.maps.geocoder(); var latlng=新的google.maps.latlng(7.5653,80.4303); 变量映射选项={ 缩放:8, 中心:拉特林, map

这段代码运行良好。我想计算我创建的直线的距离。我希望通过警报以米为单位显示此值。请帮帮我。。。(在这种情况下,两个点直接与直线连接。我想计算直线距离。)

使用(确保包括)

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

代码片段:

var地理编码器;
var映射;
函数初始化(){
geocoder=新的google.maps.geocoder();
var latlng=新的google.maps.latlng(7.5653,80.4303);
变量映射选项={
缩放:8,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“map”)、mapOptions);
createLine();
}
函数createLine(){
var address=document.getElementById('startvalue').value;
var address2=document.getElementById('endvalue').value;
var temp,temp2;
地理编码({
“地址”:地址
},功能(结果、状态){
如果(状态!=“OK”)警报(“地址的地理代码:“+address+”失败,状态=“+status”);
temp=结果[0]。geometry.location;
document.getElementById('results').innerHTML+=temp.toUrlValue()+“
”; 地理编码({ “地址”:地址2 },功能(结果、状态){ 如果(状态!=“OK”)警报(“地址的地理代码:“+address+”失败,状态=“+status”); temp2=结果[0]。geometry.location; document.getElementById('results').innerHTML+=temp2.toUrlValue()+“
”; var路由=[ 临时雇员 临时2 ]; var polyline=新的google.maps.polyline({ 路径:路线, strokeColor:#ff0000“, 笔划不透明度:0.6, 冲程重量:5 }); var lengthInMeters=google.maps.geometry.spheremic.computeLength(polyline.getPath()); //警报(“多段线为“+长度米+”长”); document.getElementById('results')。innerHTML+=“多段线是”+长度米+“米长
”; 多段线.setMap(map); var bounds=new google.maps.LatLngBounds(); 扩展(临时); 扩展(temp2); 映射边界(bounds); }); }); } google.maps.event.addDomListener(窗口“加载”,初始化)
html,
身体,
#地图{
保证金:0;
填充:0;
身高:100%;
}

使用(请确保包括以下内容)

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

代码片段:

var地理编码器;
var映射;
函数初始化(){
geocoder=新的google.maps.geocoder();
var latlng=新的google.maps.latlng(7.5653,80.4303);
变量映射选项={
缩放:8,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“map”)、mapOptions);
createLine();
}
函数createLine(){
var address=document.getElementById('startvalue').value;
var address2=document.getElementById('endvalue').value;
var temp,temp2;
地理编码({
“地址”:地址
},功能(结果、状态){
如果(状态!=“OK”)警报(“地址的地理代码:“+address+”失败,状态=“+status”);
temp=结果[0]。geometry.location;
document.getElementById('results').innerHTML+=temp.toUrlValue()+“
”; 地理编码({ “地址”:地址2 },功能(结果、状态){ 如果(状态!=“OK”)警报(“地址的地理代码:“+address+”失败,状态=“+status”); temp2=结果[0]。geometry.location; document.getElementById('results').innerHTML+=temp2.toUrlValue()+“
”; var路由=[ 临时雇员 临时2 ]; var polyline=新的google.maps.polyline({ 路径:路线, strokeColor:#ff0000“, 笔划不透明度:0.6, 冲程重量:5 }); var lengthInMeters=google.maps.geometry.spheremic.computeLength(polyline.getPath()); //警报(“多段线为“+长度米+”长”); document.getElementById('results')。innerHTML+=“多段线是”+长度米+“米长
”; 多段线.setMap(map); var bounds=new google.maps.LatLngBounds(); 扩展(临时); 扩展(temp2); 映射边界(bounds); }); }); } google.maps.event.addDomListener(窗口“加载”,初始化)
html,
身体,
#地图{
保证金:0;
填充:0;
身高:100%;
}

看看这个答案看看这个答案
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);
    });
});
}
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);
    });
});
}