Gets the latitude and longitude of the current position according to HTML5

Source: Internet
Author: User

is to make the location of your map more accurate.

Reviewed the introduction: http://www.w3school.com.cn/html5/html_5_geolocation.asp

Look at the introduction of the data is very simple.

<!DOCTYPE HTML><HTML><Body><PID= "Demo">Click this button to get your coordinates:</P><Buttononclick= "getLocation ()">Try it.</Button><Script>varx=document.getElementById ("Demo");functiongetLocation () {if(navigator.geolocation) {navigator.geolocation.getCurrentPosition (showposition); }  Else{x.innerhtml="Geolocation is isn't supported by this browser.";} }functionshowposition (position) {x.innerhtml="Latitude:" +Position.coords.latitude+   "<br/>longitude:" +Position.coords.longitude; }</Script></Body></HTML>

Right click on the button to get back latitude and longitude

One problem is: Navigator.geolocation.getCurrentPosition (showposition);  This callback, sometimes there, is sometimes not, unstable. Also is out of latitude and longitude to get on the map, I went to, ran to Guangzhou ...

--------------------------------------

itself Baidu Map API

IP location: http://lbsyun.baidu.com/jsdemo.htm#i8_2, a bit rough, showing the city name

Browser location: Http://lbsyun.baidu.com/jsdemo.htm#i8_1, get latitude and longitude display, approximate.

Baidu has a geolocation, open the above 2 in one, replace the following <script> code below, perform the following

<script type= "Text/javascript" >//Baidu Map API function var map = new Bmap.map ("Allmap"); var point = new Bmap.point (  116.331398,39.897445); Map.centerandzoom (point,12); var geolocation = new Bmap.geolocation (); Instantiates the browser anchor object. Here is the GetCurrentPosition method. Calling the object's GetCurrentPosition (), unlike HTML5, this method prototype is GetCurrentPosition (callback:function[, options:positionoptions]), That is, regardless of whether success or not is performed on the callback function 1, the second argument is about the location option. Therefore, the ability to locate success needs to be judged by itself in callback function 1. The Geolocation.getcurrentposition (function (r) {///Position result object is passed to the R variable if (this.getstatus () = = bmap_status_success) {//through Geoloc        The GetStatus () of the ation class determines whether or not to locate successfully.    var mk = new Bmap.marker (r.point);    Create marker Map.addoverlay (MK) based on the point position of this point;   Add the marker as a cover to the map map Map.panto (r.point); Moves the center point of the map to the point where it is positioned.        Note that R.point is not an R object.  Alert (' Your location: ' +r.point.lng+ ', ' +r.point.lat ');    The Point property of the R object is also an object, the LNG attribute of the object represents longitude, and the LAT attribute represents latitude.    } else {alert (' failed ' +this.getstatus ()); }},{enablehighaccuracy:true}) </script>

  

The approximate latitude and longitude are obtained, but the city is right and the location is somewhat biased.

Gets the latitude and longitude of the current position according to HTML5

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.