如何使用谷歌地图api V3来计算两个城市之间的距离 [英] How to calculate distance between two cities using google maps api V3

查看:180
本文介绍了如何使用谷歌地图api V3来计算两个城市之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算行车距离?



我无法找到一个可行的例子,任何人都可以帮我吗?


$ b

编辑:
更多信息:



我有两个输入文字输入城市。在更改中,我只想显示/更新另一个div中的距离。

解决方案

您需要的是距离矩阵API。也就是说,如果你真的在谷歌地图组合中使用它。请注意,如果Google没有在页面某处使用Google Map,则Google禁止使用此API。



以下是距离矩阵API的一个基本示例:

 <!DOCTYPE html> 
< html>
< head>
< script type =text / javascriptsrc =http://maps.google.com/maps/api/js?sensor=false>< / script>
< script type =text / javascript>
var origin = new google.maps.LatLng(55.930385,-3.118425),
destination =Stockholm,Sweden,
service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix(
{
原产地:[原产地],
目的地:[目的地],
travelMode:google.maps.TravelMode.DRIVING ,
avoidHighways:false,
avoidTolls:false
},
callback
);

函数回调(响应,状态){
var orig = document.getElementById(orig),
dest = document.getElementById(dest),
dist = document.getElementById(dist);

if(status ==OK){
orig.value = response.destinationAddresses [0];
dest.value = response.originAddresses [0];
dist.value = response.rows [0] .elements [0] .distance.text;
} else {
alert(Error:+ status);
}
}
< / script>
< / head>
< body>
< br>
使用距离矩阵的基本示例。< br>< br>
原产地:< input id =origtype =textstyle =width:35em>< br>< br>
目的地:< input id =desttype =textstyle =width:35em>< br>< br>
距离:< input id =disttype =textstyle =width:35em>
< / body>
< / html>

这是一个适合您个人品味的谷歌示例版本,我在 Google Maps API v3 - 距离矩阵部分


How to calculate the driving distance ?

I'm unable to find a working example, can anybody help me ?

Thank you.

EDIT: More info:

I have two text inputs to type the cities. On change I just want to display/update the distance in another div.

解决方案

What you want is the Distance Matrix API. That is, if you are really using it in combination google maps. Note that Google prohibits the use of this API if you are not using it with a Google Map somewhere on the page.

Here's a basic example of the Distance Matrix API:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var origin = new google.maps.LatLng(55.930385, -3.118425),
    destination = "Stockholm, Sweden",
    service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix(
    {
        origins: [origin],
        destinations: [destination],
        travelMode: google.maps.TravelMode.DRIVING,
        avoidHighways: false,
        avoidTolls: false
    }, 
    callback
);

function callback(response, status) {
    var orig = document.getElementById("orig"),
        dest = document.getElementById("dest"),
        dist = document.getElementById("dist");

    if(status=="OK") {
        orig.value = response.destinationAddresses[0];
        dest.value = response.originAddresses[0];
        dist.value = response.rows[0].elements[0].distance.text;
    } else {
        alert("Error: " + status);
    }
}
</script>
</head>
<body>
    <br>
    Basic example for using the Distance Matrix.<br><br>
    Origin: <input id="orig" type="text" style="width:35em"><br><br>
    Destination: <input id="dest" type="text" style="width:35em"><br><br>
    Distance: <input id="dist" type="text" style="width:35em">
</body>
</html>

This is a version of google's example adapted to your personal taste which I found in the Google Maps Api v3 - Distance Matrix section

这篇关于如何使用谷歌地图api V3来计算两个城市之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆