尝试使用 googlemaps api 和 javascript 查找两个 LatLng 对象之间的距离

发布于 2024-10-24 08:57:13 字数 1291 浏览 1 评论 0原文

您好,我正在使用 html5 地理定位,并使用 navigator.geolocation.getCurrentPosition(success, failed) 对象来获取用户位置。

然后,在 success 函数中,我创建了一个名为 coords 的变量来保存坐标,并创建了一个变量 coords2 来保存我编写的其他坐标。

 var coords = new google.maps.LatLng(position.coords.latitude position.coords.longitude); 
 var coords2 = new google.maps.LatLng(55.8619788, -4.2867578);

使用以下代码,我将地图上的两个点定位为标记:

 var mapOptions = {

                zoom: 16,
                center: coords,
                mapTypeControl: false,
                navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                mapTypeId: google.maps.MapTypeId.ROADMAP,

        };


 map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);  


 var marker = new google.maps.Marker({
                position: coords,
                map: map,
                title: "Your current location!"
        });


 var marker2 = new google.maps.Marker({
                position: coords2,
                map: map,
                title: "Nearest Person!"
        });

然后我想要做的是计算出这些点之间的距离。我似乎找不到一种方法可以做到这一点。

这就是我正在尝试的:

  var distance = computeDistanceBetween(coords, coords2);
  alert(distance);

知道我做错了什么吗?干杯! 保罗

Hi I am playing with html5 geolocation and have used the navigator.geolocation.getCurrentPosition(success, fail) object to get the users position.

In the success function I then created a variable called coords to hold the coordinates and a variable coords2 to hold other coordinates i made up.

 var coords = new google.maps.LatLng(position.coords.latitude position.coords.longitude); 
 var coords2 = new google.maps.LatLng(55.8619788, -4.2867578);

Using the following code I position the two points on the map as markers:

 var mapOptions = {

                zoom: 16,
                center: coords,
                mapTypeControl: false,
                navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                mapTypeId: google.maps.MapTypeId.ROADMAP,

        };


 map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);  


 var marker = new google.maps.Marker({
                position: coords,
                map: map,
                title: "Your current location!"
        });


 var marker2 = new google.maps.Marker({
                position: coords2,
                map: map,
                title: "Nearest Person!"
        });

What I then want to do is work out the distance between these points. I cant seem to find a method that does it.

Here is what I am trying:

  var distance = computeDistanceBetween(coords, coords2);
  alert(distance);

Any idead what im doing wrong?Cheers!
Paul

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

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

发布评论

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

评论(3

獨角戲 2024-10-31 08:57:13

在 v2 中,您可以执行

coords.distanceFrom(coords2);

此操作,记录在 http:// code.google.com/apis/maps/documentation/javascript/v2/reference.html#GLatLng

编辑:我认为您可能使用的是 v3。如果是这样,我相信您将需要完整的名称空间:

变量距离 =
google.maps.geometry.spherical.computeDistanceBetween(坐标,
坐标2);

In v2, you can do

coords.distanceFrom(coords2);

It's documented at http://code.google.com/apis/maps/documentation/javascript/v2/reference.html#GLatLng

Edit: I think you may be using v3. If so, I believe you'll need the full namespace:

var distance =
google.maps.geometry.spherical.computeDistanceBetween(coords,
coords2);

定格我的天空 2024-10-31 08:57:13

默认情况下不加载 google.maps.geometry 库。您必须在引导请求中显式指定加载它:

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

The google.maps.geometry library is not loaded by default. You have to explicitly specify to load it also, in the bootstrap request:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=true_or_false"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文