H5 uses Baidu map for GPS Positioning and h5mapgps Positioning
Preface
Currently, positioning is used for m-end operations. When a user opens the h5 page for the first time, gps positioning is started and Baidu map is used to locate the city. According to our logic, gps is used to locate and obtain the longitude and latitude, and then send it to the background to call Baidu's interface to find the city name.
1. query to get the city name. We can query the corresponding city id in our database based on the city name (the query will be frequent and can be queried Based on the xml cache, you can also put it in redis)
2. For the sake of insurance, we will also maintain a set of city information in our own library based on the longitude and latitude to prevent the interface from affecting positioning when it is unavailable.
H5 GPS Positioning
1 (function () {2 var 3 isGeolocation = false, 4 lat = 0, 5 lng = 0, 6 coords = null; 7 8 if (navigator. geolocation) {isGeolocation = true ;}; 9 if (isGeolocation) {10 function getPosSuccess (position) {11 coords = position. coords; 12 lat = coords. latitude, lng = coords. longpolling; 13 $. ajax ({14 type: 'get', 15 ype: 'json', 16 url: '/Home/GetPositionArea', 17 data: {'lat ': lat, 'lng ': lng}, 18 suc Cess: function (data) {19 20} 21}); 22}; 23 function getPosError (err) {24 switch (err) {25 case err. PERMISSION_DENIED: 26 console. log ("you have rejected the shared location. You can manually select a city. "); 27 break; 28 case err. POSITION_UNAVAILABLE: 29 console. log ("unable to get current location"); 30 break; 31 case err. TIMEOUT: 32 console. log ("Get location timeout"); 33 break; 34 default: 35 console. log ("Unknown error"); 36 break; 37} 38 39}; 40 navigator. geolocation. getCurrentPosition (getPosSuccess, getPosError, null); 41} else {42 43}; 44 45 })();
Query specific cities with Baidu Interfaces
Public JsonResult GetPositionArea (decimal lng, decimal lat) {string api = string. Format ("http://api.map.baidu.com/geocoder/v2? Output = json & ak = XXXXX & location = {0}, {1} ", lat, lng); // input your own business parameter ak string result = HttpRequestHelper. get (api); return Json (result, JsonRequestBehavior. allowGet );}