尝试使用 googlemaps api 和 javascript 查找两个 LatLng 对象之间的距离
您好,我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 v2 中,您可以执行
此操作,记录在 http:// code.google.com/apis/maps/documentation/javascript/v2/reference.html#GLatLng
编辑:我认为您可能使用的是 v3。如果是这样,我相信您将需要完整的名称空间:
In v2, you can do
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:
有 computeDistanceBetween()新的V3 几何库
There is the computeDistanceBetween() in the new V3 Geometry Library
默认情况下不加载 google.maps.geometry 库。您必须在引导请求中显式指定加载它:
The google.maps.geometry library is not loaded by default. You have to explicitly specify to load it also, in the bootstrap request: