Google 地图 - 如何获取两点之间的距离(以米为单位)?

发布于 2024-12-13 15:31:39 字数 155 浏览 0 评论 0原文

我有这些坐标:

(45.463688, 9.18814) 
(46.0438317, 9.75936230000002)

并且我需要(我认为通过 Google API V3)来获取这两个点之间的距离(以米为单位)。我该怎么做呢?

I have these coordinate :

(45.463688, 9.18814) 
(46.0438317, 9.75936230000002)

and I need (trought Google API V3, I think) to get the distance between those 2 points in metre. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

灯角 2024-12-20 15:31:39

如果您想使用 v3 谷歌地图 API,可以使用以下函数:
注意:您必须将 &libraries=geometry 添加到脚本源中

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

<script>
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}

</script>

If you're looking to use the v3 google maps API, here is a function to use:
Note: you must add &libraries=geometry to your script source

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

<script>
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}

</script>
静若繁花 2024-12-20 15:31:39

我认为你可以不需要任何特定的API,并使用简单的Javascript计算距离:

This< /a> 网站提供了有关地理计算和用于距离计算的 Javascript 示例的良好信息。

好吧,快速浏览一下 Google API 页面,看来您可以通过以下方式做到这一点:

  1. 调用 DirectionsService().route() 来获取 DirectionsResult 与路线
  2. 对于一条或每条路线,遍历其 Legs 属性并计算距离总和

I think you could do without any specific API, and calculate distance with plain Javascript:

This site has good info about geographical calculations and Javascript sample for distance calculation.

Ok, quick glance at Google API page and it seems, you could do it by:

  1. Call DirectionsService().route() to get DirectionsResult with routes
  2. For one or each route go through its legs property and calculate sum of distances
人事已非 2024-12-20 15:31:39

这主要是通过 google api 之外的数学来完成的,因此除了查找正确的坐标之外,您不需要使用该 API 进行任何操作。

了解了这一点,这里有一个之前回答过的与 Javascript 相关的问题的链接。

计算 2 个 GPS 坐标之间的距离

This is primarily done with math outside of the google api, so you shouldn't need to use the API for anything other than finding the correct coordinates.

Knowing that, here's a link to a previously answered question relating to Javascript.

Calculate distance between 2 GPS coordinates

黎歌 2024-12-20 15:31:39

试试这个

 CLLocationCoordinate2D coord1;
 coord1.latitude = 45.463688;
 coord1.longitude = 9.18814;
 CLLocation *loc1  = [[[CLLocation alloc] initWithLatitude:coord1.latitude longitude:coord1.longitude] autorelease];

 CLLocationCoordinate2D coord2;
 coord2.latitude = 46.0438317;
 coord2.longitude = 9.75936230000002;
 CLLocation *loc2  = [[[CLLocation alloc] initWithLatitude:46.0438317 longitude:9.75936230000002] autorelease];

 CLLocationDistance d1 = [loc1 distanceFromLocation:loc2];

Try this

 CLLocationCoordinate2D coord1;
 coord1.latitude = 45.463688;
 coord1.longitude = 9.18814;
 CLLocation *loc1  = [[[CLLocation alloc] initWithLatitude:coord1.latitude longitude:coord1.longitude] autorelease];

 CLLocationCoordinate2D coord2;
 coord2.latitude = 46.0438317;
 coord2.longitude = 9.75936230000002;
 CLLocation *loc2  = [[[CLLocation alloc] initWithLatitude:46.0438317 longitude:9.75936230000002] autorelease];

 CLLocationDistance d1 = [loc1 distanceFromLocation:loc2];
甜`诱少女 2024-12-20 15:31:39

试试这个:

const const toRadians = (val) => {
    return val * Math.PI / 180;
}
const toDegrees = (val) => {
    return val * 180 / Math.PI;
}
// Calculate a point winthin a circle
// circle ={center:LatLong, radius: number} // in metres
const pointInsideCircle = (point, circle) => {
    let center = circle.center;
    let distance = distanceBetween(point, center);

    //alert(distance);
    return distance < circle.radius;
};

const distanceBetween = (point1, point2) => {
   //debugger;
   var R = 6371e3; // metres
   var φ1 = toRadians(point1.latitude);
   var φ2 = toRadians(point2.latitude);
   var Δφ = toRadians(point2.latitude - point1.latitude);
   var Δλ = toRadians(point2.longitude - point1.longitude);

   var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
           Math.cos(φ1) * Math.cos(φ2) *
           Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

   return R * c;
}

参考文献:http://www.movable-type.co.uk /scripts/latlong.html

Try this:

const const toRadians = (val) => {
    return val * Math.PI / 180;
}
const toDegrees = (val) => {
    return val * 180 / Math.PI;
}
// Calculate a point winthin a circle
// circle ={center:LatLong, radius: number} // in metres
const pointInsideCircle = (point, circle) => {
    let center = circle.center;
    let distance = distanceBetween(point, center);

    //alert(distance);
    return distance < circle.radius;
};

const distanceBetween = (point1, point2) => {
   //debugger;
   var R = 6371e3; // metres
   var φ1 = toRadians(point1.latitude);
   var φ2 = toRadians(point2.latitude);
   var Δφ = toRadians(point2.latitude - point1.latitude);
   var Δλ = toRadians(point2.longitude - point1.longitude);

   var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
           Math.cos(φ1) * Math.cos(φ2) *
           Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

   return R * c;
}

References: http://www.movable-type.co.uk/scripts/latlong.html

回忆那么伤 2024-12-20 15:31:39

您指的是整个路径长度的距离还是只想知道位移(直线距离)?我发现没有人指出这里距离和位移之间的区别。对于距离计算由 JSON/XML 数据给出的每个路线点,对于位移,有一个使用 Spherical 类的内置解决方案。

Are you referring to distance as in length of the entire path or you want to know only the displacement (straight line distance)? I see no one is pointing the difference between distance and displacement here. For distance calculate each route point given by JSON/XML data, as for displacement there is a built-in solution using Spherical class.

仄言 2024-12-20 15:31:39

您指的是整个路径长度的距离还是只想知道位移(直线距离)?我发现没有人指出这里距离和位移之间的区别。对于距离计算由 JSON/XML 数据给出的每个路线点,对于位移,有一个使用 Spherical 类的内置解决方案。这个想法是,如果您指的是距离,那么您只需在每个路径点上使用computeDistanceBetween并将其连接起来。

//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}

Are you referring to distance as in length of the entire path or you want to know only the displacement (straight line distance)? I see no one is pointing the difference between distance and displacement here. For distance calculate each route point given by JSON/XML data, as for displacement there is a built-in solution using Spherical class. The idea is if your are referring to distance then you just use the computeDistanceBetween on each path point and concatenate it.

//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文