If you open the demo page in browser and assume you don’t have a GPS device connected to your machine, then all this web app does is to detect your location based on your IP address and add a maker on Google Maps. The source code is also as simple as a “HelloWorld” sample of W3C Geolocation API:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);} else {
error('not supported');}function success(position) {
// ...non-related code omitted...
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);var marker = new google.maps.Marker({position: latlng,map: map,title:"You are here!"
});}function error(msg) {
// ...non-related code omitted...
}
I tried this geolocation demo app twice at my bedroom, one using Firefox 3.6 and the other is using Google Chrome, and below are how the app locates me:
Firefox 3.6 Successfully locate me in my land lot
Google Chrome is a little off my land lot, but still quite accurate
The results of both are very accurate to where I actually was. That’s just amazing! And I guess the conclusion is that IP-based Geolocation can also be really accurate.
No comments:
Post a Comment