Get latitude and longitude address (copy) based on HTML5 geolocation

Source: Internet
Author: User

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.

?
varposition_option = {                enableHighAccuracy: true,                maximumAge: 30000,                timeout: 20000            };navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError, position_option);
?
function getPositionSuccess( position ){        varlat = position.coords.latitude;        varlng = position.coords.longitude;        alert( "您所在的位置: 纬度"+ lat + ",经度" + lng );        if(typeofposition.address !== "undefined"){                varcountry = position.address.country;                varprovince = position.address.region;                varcity = 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

?
functiongetPositionError(error) {    switch(error.code) {        caseerror.TIMEOUT:            alert("连接超时,请重试");            break;        case error.PERMISSION_DENIED:            alert("您拒绝了使用位置共享服务,查询已取消");            break;        caseerror.POSITION_UNAVAILABLE:            alert("获取位置信息失败");            break;    }}

Get latitude and longitude address (copy) based on HTML5 geolocation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.