如何计算折线距离? [英] how to calculate polyline distance?
问题描述
函数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来显示以米为单位的多段线值。请帮助我...(在这种情况下,两点与直接线连接,我不会计算直线距离。)
使用 google.maps.geometry.spherical.computeLength (确保包含
$ p $ 函数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);
});
});
}
工作示例(更新上一个问题中的示例以包含长度计算)
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 distance the line which i created.i wont to display polyline value in meters via alert.Please help me...(in this case two point connect with direct line.i wont to calculate that direct line distance. )
Use google.maps.geometry.spherical.computeLength (be sure to include the 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 (updated the example from your last question to include the length calculation)
这篇关于如何计算折线距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!