1. 程式人生 > >百度地圖sdk定位所在的城市名稱和街道資訊

百度地圖sdk定位所在的城市名稱和街道資訊

變數的宣告:
    String city = null;  //當前所在的城市
    String addr = null; //當前所在地點
在MyLocationListenner implements BDLocationListener 類中獲取城市名:
public class MyLocationListenner implements BDLocationListener {

                @Override
                public void onReceiveLocation(BDLocation location) {
                        // map view 銷燬後不在處理新接收的位置
                        if (location == null || mMapView == null)
                                return;
                        MyLocationData locData = http://blog.csdn.net/charlist/article/details/new MyLocationData.Builder()
                                        .accuracy(location.getRadius())
                                        // 此處設定
開發者
獲取到的方向資訊,順時針0-360
                                        .direction(100).latitude(location.getLatitude())
                                        .longitude(location.getLongitude()).build();
                        mBaiduMap.setMyLocationData(locData);
                        if (isFirstLoc) {
                                isFirstLoc = false;
                                LatLng ll = new LatLng(location.getLatitude(),
                                                location.getLongitude());
                                MapStatus
update
 u = MapStatusUpdateFactory.newLatLng(ll);
                                mBaiduMap.animateMapStatus(u);
                        }
                        try {
                                city=location.getCity();
                                addr=location.getAddrStr();
                    Toast.makeText(baidu_map.this, city+addr, 1).show(); //吐司出來的是NULL??

                        } catch (Exception e) {
                                int err = location.getLocType();
                                Toast.makeText(baidu_map.this, err, 1).show();
                        }
            

                }

如紅色部分,這樣一直無法獲取到地址資訊,提示為NULL,

需要在紅色部分上面加入:

                LocationClientOption option = new LocationClientOption();
                option.setIsNeedAddress(true);
                mLocClient.setLocOption(option);


變數的宣告:
    String city = null;  //當前所在的城市
    String addr = null; //當前所在地點
在MyLocationListenner implements BDLocationListener 類中獲取城市名:
public class MyLocationListenner implements BDLocationListener {

                @Override
                public void onReceiveLocation(BDLocation location) {
                        // map view 銷燬後不在處理新接收的位置
                        if (location == null || mMapView == null)
                                return;
                        MyLocationData locData = http://blog.csdn.net/charlist/article/details/new MyLocationData.Builder()
                                        .accuracy(location.getRadius())
                                        // 此處設定開發者獲取到的方向資訊,順時針0-360
                                        .direction(100).latitude(location.getLatitude())
                                        .longitude(location.getLongitude()).build();
                        mBaiduMap.setMyLocationData(locData);
                        if (isFirstLoc) {
                                isFirstLoc = false;
                                LatLng ll = new LatLng(location.getLatitude(),
                                                location.getLongitude());
                                MapStatusupdate u = MapStatusUpdateFactory.newLatLng(ll);
                                mBaiduMap.animateMapStatus(u);
                        }
                        try {
                                city=location.getCity();
                                addr=location.getAddrStr();
                    Toast.makeText(baidu_map.this, city+addr, 1).show(); //吐司出來的是NULL??

                        } catch (Exception e) {
                                int err = location.getLocType();
                                Toast.makeText(baidu_map.this, err, 1).show();
                        }
            

                }

如紅色部分,這樣一直無法獲取到地址資訊,提示為NULL,

需要在紅色部分上面加入:

                LocationClientOption option = new LocationClientOption();
                option.setIsNeedAddress(true);
                mLocClient.setLocOption(option);