javascript - h5 geographical location positioning getCurrentPosition, after positioning is allowed, the function will not be used after success, experts can give advice!
淡淡烟草味
2017-06-05 11:12:51
// 出发城市 let $cityname; let $config = { getPoint: function() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition($config.showPosition, $config.handleLocationError, { enableHighAccuracy: true, timeout: 1000, maximumAge: 0 }); } }, initCity: function() { // api.setLocalStorage('uzmdepartureCityName', '北京'); $cityname = '北京'; alert('失败'); }, handleLocationError: function(error) { $config.initCity(); }, showPosition: function(position) { let latitude = position.coords.latitude, longitude = position.coords.longitude; console.log(latitude, longitude); let point = new window.BMap.Point(longitude, latitude), gc = new window.BMap.Geocoder; gc.getLocation(point, function(rs) { try { let locationCity = rs.addressComponents.province; $cityname = locationCity; // alert('您现在的城市是:' + $cityname); $doc.getElementById('mobile').value = $cityname; } catch (e) { $config.initCity(); } }); } }; $config.getPoint();
This has something to do with the browser you used for testing. Last year, the project needed to be studied for a period of time, but there was no satisfactory result. As far as I understand, some browsers use Google’s location services, and Google services are blocked in China. So either the positioning is very, very slow and very inaccurate, or it is simply impossible to position. . . You can tell the difference by trying different browsers. . . It is recommended to use third-party JS positioning services such as AutoNavi. Those that have undergone special processing will be much better.