作百度地圖定位時,location.getAddrStr()獲取到的值一直為空的問題
阿新 • • 發佈:2019-02-11
在做百度地圖定位時,在MyLocationListener介面的回撥方法中,具體如下:
public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location!=null&flag) { flag=false; currentLag = location.getLatitude();//經度 currentLng = location.getLongitude();//維度 currentAddr = location.getAddrStr();//位置 Toast.makeText(MainActivity.this, "當前位置"+currentAddr, Toast.LENGTH_SHORT).show(); // 構造定位資料 MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此處設定開發者獲取到的方向資訊,順時針0-360 .direction(0).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); // 設定定位資料 mBaiduMap.setMyLocationData(locData); MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING,true, null); mBaiduMap.setMyLocationConfigeration(config); /*當不需要定點陣圖層時關閉定點陣圖層 mBaiduMap.setMyLocationEnabled(false);*/ // 設定到當前位置 mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),16));//設定縮放級別 } } }
在以上程式碼中,location.getAddrStr()獲取到的地理位置一直為空,之所以出現這樣的問題,是在配置定位SDK引數配置時少些了一句:
option.setIsNeedAddress(true);//可選,設定是否需要地址資訊,預設不需要
把以上這句加上就可以了!!!