简体   繁体   English

使用Google Map API计算两个位置之间的距离

[英]Calculate the distance between two location using google map api

I have a requirement to calculate and print the distance from current location to given destination. 我需要计算并打印从当前位置到给定目的地的距离。 As per my understanding, i can achieve this by google Map api. 根据我的理解,我可以通过Google Map API实现这一目标。

So, I will have to pass only destination location and api will the map as well as distance. 因此,我将只需要传递目的地位置,并且api会传递地图以及距离。

For that I have configured on google and got the below link, I am not able to understand how to use this, but in below url I am passing the source and destination, but I don't want to pass the source, it should be taken current location as source. 为此,我已经在google上配置并获得了以下链接,我无法理解如何使用它,但是在url下面,我正在传递源和目标,但是我不想传递源,它应该是以当前位置为来源。

Is it possible to print the distance on web page? 可以在网页上打印距离吗?

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

It looks like you are looking for a Directions service but since: 您似乎在寻找路线服务,但由于:

but I don't want to pass the source, it should be taken current location as source. 但我不想传递来源,应该将当前位置作为来源。

below is provided the modified example from Directions service page that demonstrates how to specify current location for 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 的jsfiddle

Here is an example on how to do this: 这是有关如何执行此操作的示例:

Code: 码:

var p1 = new google.maps.LatLng(20, 9.4);
var p2 = new google.maps.LatLng(60.23, 9.7);

function distanceTwoPoints(p3, p4){
  return (google.maps.geometry.spherical.computeDistanceBetween(p3, p4) / 1000); //dividing by 1000 to get Kilometers
}
alert(distanceTwoPoints(p1, p2));

Fiddle example (UPDATED) : https://jsfiddle.net/wexd3spp/12/ 小提琴示例(UPDATED)https : //jsfiddle.net/wexd3spp/12/

If you want to print it on the page I suggest to use the standard output method: 如果要在页面上打印,建议使用标准输出方法:

document.getElementById("demo").innerHTML = distanceTwoPoints(p1,p2)+" Km";

Here is what the documentation says about it: 以下是文档说明的内容:

The distance between two points is the length of the shortest path between them. 两点之间的距离是两点之间最短路径的长度。 This shortest path is called a geodesic. 该最短路径称为测地线。 On a sphere all geodesics are segments of a great circle. 在球体上,所有测地线都是一个大圆的段。 To compute this distance, call computeDistanceBetween(), passing it two LatLng objects. 要计算此距离,请调用computeDistanceBetween(),并向其传递两个LatLng对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Google API获取两个位置之间的距离。 的PHP - Getting Distance between two location using Google API. PHP 使用Google Maps API V3计算当前位置与不同位置之间的距离 - Calculate distance between current location and and a different location using Google Maps API V3 是否可以使用Google Maps API V3通过陆运计算两个英国邮政编码之间的距离? - Is it possible to calculate distance by land transport between two UK postcodes using Google Maps API V3? 使用Google Maps API DistanceMatrixService计算API中当前位置和用户位置之间的距离 - Calculate the distance between my current location and user locations from API with Google Maps API DistanceMatrixService 计算2个位置谷歌地图Api之间的距离 - Calculate the distance between 2 position Google maps Api 计算用户位置与地图上所有标记之间的距离 - Calculate distance between users location and all the markers on map 使用google gps api和php获取两点之间的距离 - getting distance between two points using google gps api with php 如何使用我的位置使用经度和纬度计算两点之间的距离 - How to use my location to calculate distance between two points using latitude and longitude 如何使用Google Maps API计算两个以上的点之间的距离? - How to calculate distance between more than two points with google maps api? 如何使用谷歌地图 API V2 计算总距离 - How to calculate total distance with Google map API V2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM