Baidu Map API, users refuse to get geographic location getStatus() or BMAP_STATUS_SUCCESS

Keywords: Javascript

1. Business Scenario: One of the methods in Baidu Map JavaScript API is to locate through browser, getCurrentPosition. But users refuse or allow access to geographical location, this.getStatus() is 0;

  1. var myposition;  
  2.     var myposition_lng;  
  3.     var myposition_lat;  
  4.  var geolocation = new BMap.Geolocation();  
  5.   geolocation.getCurrentPosition(function(r){  
  6.     if(this.getStatus() == BMAP_STATUS_SUCCESS){  
  7.         var mk = new BMap.Marker(r.point);  
  8.         map.addOverlay(mk);  
  9.         map.panTo(r.point);  
  10.         myposition_lat=r.point.lat;  
  11.         myposition_lng=r.point.lng;  
  12.         alert('accuracy:'+r.accuracy);  
  13.         myposition=new BMap.Point(r.point.lng,r.point.lat);  
  14.     }else {  
  15.         alert('failed'+this.getStatus());  
  16.     }          
  17.   },{enableHighAccuracy: true})  



2. Because when users refuse to use the browser's location, Baidu Map will get the approximate location by other means, so this.getStatus() is 0.


3. Solution: When the user refuses to use the browser location, the accuracy is null. By this value, the user can judge whether the user refuses to use the site to obtain the browser location information.




4. The code is as follows

  1. var myposition;  
  2.     var myposition_lng;  
  3.     var myposition_lat;  
  4.  var geolocation = new BMap.Geolocation();  
  5.   geolocation.getCurrentPosition(function(r){  
  6.     if(this.getStatus() == BMAP_STATUS_SUCCESS){  
  7.         var mk = new BMap.Marker(r.point);  
  8.         map.addOverlay(mk);  
  9.         map.panTo(r.point);  
  10.         myposition_lat=r.point.lat;  
  11.         myposition_lng=r.point.lng;  
  12.         //alert('accuracy:'+r.accuracy);  
  13.         myposition=new BMap.Point(r.point.lng,r.point.lat);  
  14.         if(r.accuracy==null){  
  15.             alert('accuracy null:'+r.accuracy);  
  16.             //Users Reject Geographical Location Authorization  
  17.             return;  
  18.         }  
  19.         //User Allow Geographical Location Authorization  
  20.     }else {  
  21.         alert('failed'+this.getStatus());  
  22.     }          
  23.   },{enableHighAccuracy: true})  

Posted by detrox on Sat, 09 Feb 2019 17:03:31 -0800