热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用谷歌地图api计算两点之间的距离?-workingoutdistancesbetweentwopointsusinggooglemapsapi?

Isitpossibletosendtwolatlongpointstogoogletocalculatethedistancebetweenthetwo?是否有可

Is it possible to send two lat long points to google to calculate the distance between the two?

是否有可能向谷歌发送两个lat长点来计算两者之间的距离?

8 个解决方案

#1


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

如果您正在使用v3 google maps API,请使用以下函数:注意:您必须在脚本源中添加&libraries = geometry


Now the function:

现在功能:

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

#2


What you're after is the Haversine formula. You don't need Google Maps to do this, you can work it out separately. There's a script to do this (in Javascript) here.

你所追求的是Haversine公式。您不需要Google地图来执行此操作,您可以单独进行操作。这里有一个脚本(在Javascript中)。

#3


Yes google can do this

是的谷歌可以做到这一点

google api docs

谷歌api文档

here's a piece of java script that get's the distance in km's for two points

这是一段java脚本,以km为单位得到两点的距离

function initialize() {
      if (GBrowserIsCompatible()) {
          map = new GMap2(document.getElementById("map_canvas"));
          map.setCenter(new GLatLng(52.6345701, -1.1294433), 13);
          map.addControl(new GLargeMapControl());          
          map.addControl(new GMapTypeControl());
          geocoder = new GClientGeocoder();

          // NR14 7PZ
          var loc1 = new GLatLng(52.5773139, 1.3712427);
          // NR32 1TB
          var loc2 = new GLatLng(52.4788314, 1.7577444);          
          alert(loc2.distanceFrom(loc1) / 1000);
      }
    }

#4


And the distance cal in c#:

和c#中的距离cal:

// this returns the distance in miles. for km multiply result: * 1.609344
public static double CalculateDistance(double lat1, double lon1, double lat2, double lon2)
{
    double t = lon1 - lon2;
    double distance = Math.Sin(Degree2Radius(lat1)) * Math.Sin(Degree2Radius(lat2)) + Math.Cos(Degree2Radius(lat1)) * Math.Cos(Degree2Radius(lat2)) * Math.Cos(Degree2Radius(t));
    distance = Math.Acos(distance);
    distance = Radius2Degree(distance);
    distance = distance * 60 * 1.1515;

    return distance;
}

private static double Degree2Radius(double deg)
{
    return (deg * Math.PI / 180.0);
}

private static double Radius2Degree(double rad)
{
    return rad / Math.PI * 180.0;
}

#5


No, but you can use Vincenty's formula, which models the shape of the Earth. It's available in Javascript here.

不,但你可以使用Vincenty的公式来模拟地球的形状。它在Javascript中可用。

A quite easy to parse web page is (e.g.): http://boulter.com/gps/distance/?from=51.5329855+-0.1303506&to=40.757584+-73.985642&units=m

一个非常容易解析的网页是(例如):http://boulter.com/gps/distance/?from = 51.5329855 +-0.1303506&to = 40.757584+-73.985642 &units=m

For people who only want a quick measure of distance, check out this gadget. However, it only uses the great circle calculation.

对于只想快速测量距离的人,请查看此小工具。但是,它只使用大圆计算。


As Rob notes, Google have added it, but they still only use the great circle formula, which can be inaccurate by 1/300.

正如Rob所说,谷歌已经添加了它,但他们仍然只使用大圈公式,这可能是1/300不准确。

#6


Its been many years since I did this but you could use the Great Circle Distance

自从我做了这么多年以来,你可以使用Great Circle Distance

#7


If you just want to get the distance as a number, try something like this.

如果您只想将距离作为数字,请尝试这样的事情。

function InitDistances() {
    var startLocation = new GLatLng(startLat, startLon);
    var endLocation = new GLatLng(endLat, endLon);
    var dist = startLocation .distanceFrom(endLocation);

    // Convert distance to miles with two decimal points precision
    return (dist / 1609.344).toFixed(2);
}

#8


there is a handy function

有一个方便的功能

http://code.google.com/apis/maps/documentation/reference.html#GLatLng.distanceFrom

basically create 2 GlatLng objects then use the above method

基本上创建2个GlatLng对象然后使用上面的方法


推荐阅读
author-avatar
张浩杰_Hh
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有