HTML5 obtains location information and html5 obtains location information.
HTML5 provides the Geolocation API to determine the user location. We can use this feature to develop applications based on geographic location information. This article uses examples to show you how to use HTML5 and use Baidu and Google map interfaces to obtain accurate geographical location information.
How to Use the HTML5 location locating Function
Geolocation is a new feature of HTML5. Therefore, it only runs on modern browsers that support HTML5, especially handheld devices such as the iphone. First, we need to check whether the browser of the user's device supports geographic location. If so, we need to obtain geographic information. Note that this feature may infringe on user privacy. Unless agreed by the user, the user location information is unavailable. Therefore, when accessing this application, we will prompt whether to allow geographic location, of course, you can select allow.
Function getLocation () {if (navigator. geolocation) {navigator. geolocation. getCurrentPosition (showPosition, showError);} else {alert ("the browser does not support location. ");}}
The code above shows that if your device supports geographic location, run the getCurrentPosition () method. If getCurrentPosition () runs successfully, a coordinates object is returned to the function specified in the showPosition parameter. The second parameter showError of the getCurrentPosition () method is used to handle errors, it specifies the function that runs when the user location fails to be obtained.
Let's take a look at the showError () function, which specifies how to handle some error codes that fail to get users' geographic locations:
Function showError (error) {switch (error. code) {case error. PERMISSION_DENIED: alert ("location failed, user refused to request location"); break; case error. POSITION_UNAVAILABLE: alert ("location failed, location information is unavailable"); break; case error. TIMEOUT: alert ("locating failed, request to get user location TIMEOUT"); break; case error. UNKNOWN_ERROR: alert ("locating failed, locating system failed"); break ;}}
Let's take a look at the showPosition () function. Call the latitude and longpolling functions of coords to obtain the user's latitude and longitude.
Function showPosition (position) {var lat = position. coords. latitude; // latitude var lag = position. coords. longpolling; // longitude alert ('latitude: '+ lat +', longitude: '+ lag );}
Use Baidu maps and Google Maps to get user addresses
We learned above that HTML5 Geolocation can obtain the user's latitude and longitude, so what we need to do is to convert the abstract latitude and longitude into a readable and meaningful real user geographic location information. Fortunately, Baidu maps and Google Maps provide this interface. We only need to pass the latitude and longitude information obtained by HTML5 to the map interface, and then return the geographical location of the user, including provincial and municipal information, and even detailed geographic location information such as street and house number.
First, define the div to display the geographical location on the page, and define id # baidu_geo and id # google_geo respectively. We only need to modify the key function showPosition (). First, let's look at Baidu map interface interaction. We send the latitude and longitude information to Baidu map interface via Ajax, and the interface will return the corresponding provincial and municipal street information. The Baidu map interface returns a string of JSON data. We can display the required information to div # baidu_geo as needed. Note that the jQuery library is used here. You need to load the jQuery library file first.
Function showPosition (position) {var latlon = position. coords. latitude + ',' + position. coords. longpolling; // baiduvar url = "baidu.com/geocoder/v2 /? Ak = C93b5178d7a8ebdb830b9b557abce78b & callback = renderReverse & location = "+ latlon +" & output = json & pois = 0 "; $. ajax ({type: "GET", dataType: "jsonp", url: url, beforeSend: function () {$ ("# baidu_geo" ).html ('positioning... ');}, success: function (json) {if (json. status = 0) {$ ("# baidu_geo" ).html (json. result. formatted_address) ;}}, error: function (XMLHttpRequest, textStatus, errorThrown) {$ ("# baidu_geo" ).html (latlon + "failed to get address location ");}});});
Let's take a look at Google map interface interaction. Similarly, we send the latitude and longitude information to the Google map interface through Ajax, and the interface will return the corresponding provincial and municipal street details. The Google map interface also returns a string of JSON data, which is more detailed than the Baidu map interface. We can display the required information to div # google_geo as needed.
Function showPosition (position) {var latlon = position. coords. latitude + ',' + position. coords. longpolling; // googlevar url = 'cn/maps/api/geocode/json? Latlng = '+ latlon +' & language = cn'; $. ajax ({type: "GET", url: url, beforeSend: function () {$ ("# google_geo" ).html ('positioning... ');}, success: function (json) {if (json. status = 'OK') {var results = json. results; $. each (results, function (index, array) {if (index = 0) {$ ("# google_geo" ).html (array ['formatted _ address']) ;}}}, error: function (XMLHttpRequest, textStatus, errorThrown) {$ ("# google_geo" ).html (latlon + "An error occurred while obtaining the address location ");}});}
The above code integrates Baidu map interface and Google map interface into the function showPosition (), which can be called according to the actual situation. Of course, this is just a simple application. We can develop many complicated applications based on this simple example. We recommend that you use a mobile browser to access the DEMO.