Recent project needs, a little research on HTML5 get the current location of the problem.
Getting the latitude and longitude of the current position is simple, a code is done
[JavaScript]View Plaincopy print?
- Navigator.geolocation.getCurrentPosition (function (position) {
- longitude = position.coords.longitude;
- latitude = position.coords.latitude;
- });
Then consult the Baidu Map API, very easy, but also a few lines of code on the matter
[JavaScript]View Plaincopy print?
- var map = new Bmap.map ("Allmap"); <pre name="code" class="JavaScript" > var point = new Bmap.point (longitude,latitude); <pre name= "code" class="JavaScript" > var GEOC = N EW Bmap.geocoder ();
[JavaScript]View Plain Copy print?
- Geoc.getlocation (Point, function (RS) {
- var addcomp = rs.addresscomponents;
- Alert (addcomp.province + "," + addcomp.city + ", " + addcomp.district + "," + Addcomp.street + "," + Ad Dcomp.streetnumber);
- });
OK, I thought this was the end, the actual test found a bit of a problem, the location of the display and my real position deviation of about two or three km or so.
At first thought it was HTML5 to get the latitude and longitude deviation problem, but not
And then began to look for information, inadvertently found a called coordinate transformation of things, a search to know, the original Baidu map to the coordinates of the depth of encapsulation, must through the interface he provided to coordinate transformation only line, nonsense not to say, on the final code bar
[JavaScript]View Plaincopy print?
- var map = new Bmap.map ("Allmap");
- var longitude, Latitude;
- Navigator.geolocation.getCurrentPosition (function (position) {
- longitude = position.coords.longitude;
- latitude = position.coords.latitude;
- });
- SetTimeout (function () {
- var gpspoint = New Bmap.point (longitude, latitude);
- BMap.Convertor.translate (gpspoint, 0, function (point) {
- var GEOC = new Bmap.geocoder ();
- Geoc.getlocation (Point, function (RS) {
- var addcomp = rs.addresscomponents;
- Alert (addcomp.province + "," + addcomp.city + ", " + addcomp.district + "," + Addcomp.street + "," + Ad Dcomp.streetnumber);
- });
- });
- }, 3000);
This code relies on two packages
[HTML]View Plaincopy print?
- <script type="Text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak= secret key"> </Script>
- <script type="Text/javascript" src= "http://developer.baidu.com/map/jsdemo/demo/ Convertor.js "></script>
Use HTML5 to get the latitude and longitude of the current phone, and access Baidu Map API, query the current location