本文主要是介绍百度地图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());
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);
这篇关于百度地图sdk定位所在的城市名称和街道信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!