One of the most interesting aspects of development work for a web development programmer is getting geographic information; Just imagine, where is the user browsing your Web page? Programmers can adjust the site's language, specific product introductions, etc. based on the user's geographic information. Here's what we're going to demonstrate by using the JavaScript geographic information API in the browser to get detailed geographic information!
Check to see if your browser supports the Geographic information API
The current mainstream browsers have a good support for JavaScript geographic information APIs. However, if you are not confident, then, the best way to confirm the geographic Information API support is the browser's functional feature testing.
if ("Geolocation" in navigator) {
//w00t!
}
else {
alert ("very unfortunate!) Your browser does not support Geolocation API features ");
}
It is important to determine whether the browser supports Geolocation APIs, and the main thing is to look at the Navigator.geolocation object, using in instead of simply using if (navigator.geolocation), Because it is possible for the latter to initialize the geographic information object, the device resource is occupied/locked.
Query Geographic Information
This navigator.geolocation.getCurrentPosition method is one of the most critical interfaces for obtaining detailed location information:
if ("Geolocation" in navigator) {
navigator.geolocation.getCurrentPosition (function (position) {
Console.log (position);}
Once you call this method (if the request succeeds, it executes the callback method that you provided in the parameter), the browser asks the user if they want to allow the program to get their geographic information.
When the user runs the Web page to get their location information, the browser can begin to read the geographic information, it will return you a location information object, the structure of the object is basically this:
"Position" Object
{
coords: {"Coordinates" object
accuracy:65,
altitude:294.4074401855469,
Altitudeaccuracy:10,
heading:-1,
latitude:43.01256284360166,
Longitude: -89.44531987692744,
Speed:-1
},
timestamp:1429722992094269
}
If you think that these geographical information (geo-longitude coordinates) is not enough, and you want these geographical coordinates belong to which country, the city, then you need to call the other Third-party database-here we do not detail.
This Geographic information API is the most common API application in many mobile applications, and as a web programmer, it should be a knowledge skill you must have. Fortunately, all the popular browsers now support this technology.
The above is the entire content of this article, I hope to learn JavaScript program to help you.