Geolocation to get the latitude and longitude address
In the past, if you want to obtain the location of the Internet users based on the user's IP address to obtain the geographical position, so that the data obtained and real data is very large deviation. To get a more precise position, you can use the HTML5 geolocation to get the latitude and longitude, and then get the location, how to get it, I will say below. First, the basic concept.
Geolocation in the Navigator object, we can use it through navigator.geolocation. Browsers that do not support geolocation do not include this object, so you can use the following code to do the ability to detect different browsers do different processing. When accessing the Geolocation object, called geolocation, the browser pops up a prompt asking the user if the location service provided by the website will be allowed to continue, or it will be stopped, only after the user's permission has been granted.
Common Navigator.geolocation objects are available in the following three ways:
Get current Location:navigator.geolocation.getCurrentPosition(success_callback_function, Error_callback_function, Position_options)
Continuous acquisition of geographic location:navigator.geolocation.watchPosition(success_callback_function, Error_callback_function, Position_options)
Clear persistent Get geo-location event:navigator.geolocation.clearWatch(watch_position_id)
The parameter position_options is a configuration item that is passed in the JSON format:
Enablehighaccuracy:true/false, it will tell the browser whether to enable high-precision devices, so-called high-precision devices include but not limited to the above mentioned GPS and WIFI, when the value is true, the browser will try to enable these devices, the default means true, In this case, the browser will be as far as possible to make a more accurate query, in short, if the user has a GPS device available, will return to the GPS device query results, IP is the last choice, for mobile devices, network access point (Base station) may be another option, I have not fully understood, But according to the test, the phone without any additional functions can get more accurate results.
Maximumage: Unit milliseconds, tells the device cache time, mainly for the device to save power or bandwidth-saving aspects.
Timeout: unit milliseconds, timeout event, when getting location information beyond the set length, will trigger an error, the function that catches the error is called, and the error code points to timeout.
?
var
position_option = {
enableHighAccuracy:
true
,
maximumAge: 30000,
timeout: 20000
};
navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError, position_option);
|
?
function getPositionSuccess( position ){
var
lat = position.coords.latitude;
var
lng = position.coords.longitude;
alert(
"您所在的位置: 纬度"
+ lat +
",经度" + lng );
if
(
typeof
position.address !==
"undefined"
){
var
country = position.address.country;
var
province = position.address.region;
var
city = position.address.city;
alert(
‘ 您位于 ‘
+ country + province +
‘省‘
+ city +
‘市‘
);
}
}
|
Coords Other return information:
Coords.accuracy: Accuracy of return latitude and longitude (m)
Coords.speed: Speed
Coords.altitude: Current height, altitude (m)
Coords.altitudeaccuracy: Height accuracy (m)
Coords.heading: Facing
?
function
getPositionError(error) {
switch
(error.code) {
case
error.TIMEOUT:
alert(
"连接超时,请重试"
);
break
;
case error.PERMISSION_DENIED:
alert(
"您拒绝了使用位置共享服务,查询已取消"
);
break
;
case
error.POSITION_UNAVAILABLE:
alert(
"获取位置信息失败"
);
break
;
}
}
|
Get latitude and longitude address (copy) based on HTML5 geolocation