我在我的网站上使用这个脚本:
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
(function () {
var directionsService = new google.maps.DirectionsService(),
directionsDisplay = new google.maps.DirectionsRenderer(),
createMap = function (start) {
var travel = {
origin : (start.coords)? new google.maps.LatLng(start.lat, start.lng) : start.address,
destination : "Warszawa, al. jerozolimskie 123",
travelMode : google.maps.DirectionsTravelMode.DRIVING
// Exchanging DRIVING to WALKING above can prove quite amusing :-)
},
mapOptions = {
zoom: 10,
// Default view: downtown Stockholm
center : new google.maps.LatLng(59.3325215, 18.0643818),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("map-directions"));
directionsService.route(travel, function(result, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
};
// Check for geolocation support
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
// Success!
createMap({
coords : true,
lat : position.coords.latitude,
lng : position.coords.longitude
});
},
function () {
// Gelocation fallback: Defaults to Stockholm, Sweden
createMap({
coords : false,
address : "Polska, Warszawa"
});
}
);
}
else {
// No geolocation fallback: Defaults to Lisbon, Portugal
createMap({
coords : false,
address : "Polska, Warszawa"
});
}
})();
</script>我有问题与哈维辛公式,谁计算之间的道路距离我的位置和设定的位置…我不需要它上的任何地图或道路线-只需要公里数:)
有人能帮我吗?
非常感谢,
发布于 2013-03-12 20:47:55
我检查了APi找到了你想要的。
在此之前
directionsService.route(
请务必设置:
unitSystem: google.maps.UnitSystem.METRIC
在您的旅行变量中。
然后在调用路由之后,结果是一个DirectionsResult对象。在那里,您将拥有一个具有距离属性的数组DirectionsLeg的DirectionRoute数组。
为您的问题获取此命令应按如下方式工作:
变量结果= result.rows.elements;
var distance = results.distance.text;
distance.text返回"100 km",而as distance.value只返回"100",这是您喜欢的。
当然,迭代这些数组会更好;) (一定要实现对这些数组的检查,因为距离并不总是填充的,在某些情况下距离可能是未知的)
有关该接口的更多信息可在此处找到:https://developers.google.com/maps/documentation/javascript/directions?hl=en
PS:请确保使用接口的实际版本V3,生成您自己的密钥并使用它。https://developers.google.com/maps/documentation/javascript/tutorial?hl=en#api_key
https://stackoverflow.com/questions/15361034
复制相似问题