admin管理员组文章数量:1317579
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 = puteDistanceBetween(coords, coords2);
alert(distance);
Any idead what im doing wrong?Cheers! Paul
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 = puteDistanceBetween(coords, coords2);
alert(distance);
Any idead what im doing wrong?Cheers! Paul
Share Improve this question asked Mar 17, 2011 at 23:09 paul elliotpaul elliot 411 silver badge3 bronze badges3 Answers
Reset to default 3In v2, you can do
coords.distanceFrom(coords2);
It's documented at http://code.google./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.puteDistanceBetween(coords, coords2);
There is the puteDistanceBetween() in the new V3 Geometry Library
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./maps/api/js?libraries=geometry&sensor=true_or_false"></script>
版权声明:本文标题:Trying to find the distance between two LatLng objects using googlemaps api and javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742020161a2414456.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论