gpt4 book ai didi

javascript - 使用google map api计算两个位置之间的距离

转载 作者:行者123 更新时间:2023-11-30 16:28:55 27 4
gpt4 key购买 nike

我需要计算并打印从当前位置到给定目的地的距离。据我了解,我可以通过 google Map api 实现这一点。

因此,我将只需要传递目的地位置,而 api 将传递 map 和距离。

为此,我在谷歌上进行了配置并获得了以下链接,我无法理解如何使用它,但在下面的网址中我传递了源和目标,但我不想传递源,应该以当前位置为源。

是否可以在网页上打印距离?

https://maps.googleapis.com/maps/api/directions/json?origin=kanpur&destination=lucknow&key=%20AIzaSyCeBdq7rr-R7w7vZCXscLWgEDb3oO9CUhw

最佳答案

看起来您正在寻找 Directions service但是因为:

but I don't want to pass the source, it should be taken current location as source.

下面提供了来自 Directions service修改示例演示如何为路线服务指定当前位置的页面

function initMap() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: { lat: 41.85, lng: -87.65 }
});
directionsDisplay.setMap(map);

var printRoute = function () {
calculateAndDisplayRoute(directionsService, directionsDisplay);
};

getCurrentLocation(function (loc) {
if (loc != null) {
document.getElementById('startInner').value = loc.toString();
document.getElementById('start').innerHTML = 'Current location';
map.setCenter(loc);
}
else {
document.getElementById('start').innerHTML = 'Current location not found';
document.getElementById('btnCalc').disabled = true;
}
});

document.getElementById('btnCalc').addEventListener('click', printRoute);
}

function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route({
origin: document.getElementById('startInner').value,
destination: document.getElementById('end').value,
travelMode: google.maps.TravelMode.DRIVING
}, function (response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}


function getCurrentLocation(complete) {
// Try W3C Geolocation (Preferred)
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var location = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
complete(location);
}, function () {
complete(null);
});
}
else {
complete(null);
}
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}

#map {
height: 100%;
}

#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
<div id="floating-panel">
<b>Start: </b>
<span id="start">Current Location</span>
<b>End: </b>
<input id="end" type="text" ></input>
<input id="startInner" type="hidden"></input>
<button id="btnCalc">Print route</button>
</div>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>

JSFiddle

关于javascript - 使用google map api计算两个位置之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com