運用Google Map API(Distance Matrix Service)取得旅程時間及距離

<script async defer 
src=”https://maps.googleapis.com/maps/api/js?key=Your API key&libraries=places,drawing,geometry&v=3&callback=initMap">
</script>
let service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [{lat: 25.038935, lng: 121.5018988}],
destinations: [{lat: 25.04081, lng: 121.506566}],
travelMode:
google.maps.TravelMode[WALKING],
unitSystem:
google.maps.UnitSystem.METRIC,
}, function(response, status) {
if (status !== google.maps.DistanceMatrixStatus.OK) {
window.alert('Error was' + status);
} else {
console.log(response);
}
});

單一起點步行到達單一目的地

{
origins: [{lat: 25.038935, lng: 121.5018988}],
destinations: [{lat: 25.04081, lng: 121.506566}],
travelMode: google.maps.TravelMode[WALKING],
unitSystem: google.maps.UnitSystem.METRIC,
}
{
destinationAddresses:
["No 205, Hanzhong Street,Wanhua District,TaipeiCity,Taiwan 108"],
originAddresses:
["No 131, Yongfu Street,Wanhua District,Taipei City,Taiwan 108"],
rows: [{
elements: [{
distance: {text: “0.6 km”, value: 636},
duration: {text: “8 mins”, value: 502},
status: “OK”,
}]
}]
}

單一起點步行到多個目的地

{
origins: [{lat: 25.038935, lng: 121.5018988}],
destinations:
[
{lat: 25.04081, lng: 121.506566},
{lat: 25.0418973, lng: 121.5047744}
],
travelMode: google.maps.TravelMode[WALKING],
unitSystem: google.maps.UnitSystem.METRIC,
}
{
destinationAddresses:
[
"No. 205, Hanzhong Street,Wanhua District,TaipeiCity,Taiwan 108",
"No. 85, Guangzhou Street,Wanhua District,Taipei City,Taiwan 108" ],
originAddresses:
["No 131, Yongfu Street,Wanhua District,Taipei City,Taiwan 108"],
rows: [{
elements: [
{
distance: {text: “0.6 km”, value: 636},
duration: {text: “8 mins”, value: 502},

status: “OK”,
},
{
distance: {text: “0.4 km”, value: 447},
duration: {text: “6 mins”, value: 348},
status: “OK”,
}]
}]
}

多起點步行到多個目的地

{
origins: [
{lat: 25.038935, lng: 121.5018988},
{lat: 25.042325, lng: 121.505769}
],
destinations:
[
{lat: 25.04081, lng: 121.506566},
{lat: 25.0418973, lng: 121.5047744}
],
travelMode: google.maps.TravelMode[WALKING],
unitSystem: google.maps.UnitSystem.METRIC,
}
{
destinationAddresses:
["No. 205, Hanzhong Street,Wanhua District,TaipeiCity,Taiwan 108",
"No. 85, Guangzhou Street,Wanhua District,Taipei City,Taiwan 108" ],
originAddresses:
["No 131, Yongfu Street,Wanhua District,Taipei City,Taiwan 108",
"No. 2, Lane 82,Xining South Road,Wanhua District,Taipei City, Taiwan 108"
],
rows: [{
elements: [
{
distance: {text: “0.6 km”, value: 636},
duration: {text: “8 mins”, value: 502},

status: “OK”,
},
{
distance: {text: “0.4 km”, value: 447},
duration: {text: “6 mins”, value: 348},
status: “OK”,
}],
elements: [
{
distance: {text: “0.3 km”, value: 266},
duration: {text: “4 mins”, value: 211},

status: “OK”,
},
{
distance: {text: “0.8 km”, value: 776},
duration: {text: “10 mins”, value: 619},
status: “OK”,
}]
}]
}

--

--

Programming Skill learner and Sharer | Ruby on Rails | Golang | Vue.js | Web Map API

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
icelandcheng

icelandcheng

67 Followers

Programming Skill learner and Sharer | Ruby on Rails | Golang | Vue.js | Web Map API