javascript - 在 Map v3 API 中无法计算距离(两个坐标之间,不考虑骑行、驾驶等旅行模式)
问题描述
我想要两个地址之间的确切地理距离,它在 v3 中的旅行模式中有所不同。请找到以下代码,我调用的 API 文件是 script type='text/javascript' src='//maps.googleapis.com/maps/api/js?key=xxxxxxxxxxx'
function showLocation(a,b)
{
alert(a.value);
geocoder = new google.maps.Geocoder();
geocoder.geocode(document.forms[0].address.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the first address");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(a.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the second address");
}
else
{
location2= {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
var x=calculateDistance();
b.value = x;
}
});
}
});}
function calculateDistance(){
try
{
var glatlng1 = new GLatLng(location1.lat, location1.lon);
var glatlng2 = new GLatLng(location2.lat, location2.lon);
var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(2);
return miledistance;
}
catch (error)
{
alert(error);
}}
解决方案
推荐阅读
- python - 这两个简单的代码有什么区别?更具体地说,这个 if 语句是做什么用的?
- javascript - 从字典中选择的随机项目除了最后一个
- git - 无法删除幽灵分支 github/BLOB
- reactjs - 嵌入在反应中的 instagram 的引导问题
- c++ - 禁用模板类的复制构造函数
- json - 使用 VB.Net 解析复杂的 JSON 字符串
- powershell - 我们如何在 powershell 中使用 minimatch?
- google-forms - 谷歌表单地址输入验证
- xamarin.forms - 如何使用 Xamarin.Forms 检测 Android 设备上的传入彩信?
- python - 加载新数据后刷新 QTableView