百度地图定位
声明成员变量
private boolean isFirstLoc = true;//首次
private boolean isRequest = false;//手动请求
//ui
private MapView mMapView = null;
private ImageView img_pos;
private MapView mMapView = null;
private BaiduMap mBaiduMap;
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
if (location.getLocType() == BDLocation.TypeGpsLocation){
// GPS定位结果
locationSucess(location);
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
// 网络定位结果
locationSucess(location);
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
// 离线定位结果
locationSucess(location);
} else if (location.getLocType() == BDLocation.TypeServerError) {
ToastUtil.showToast(getApplicationContext(), "定位失败");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
ToastUtil.showToast(getApplicationContext(), "网络不通导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
ToastUtil.showToast(getApplicationContext(), "无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
}
private void locationSucess(BDLocation location) {
LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
if (isRequest || isFirstLoc){//isRequest = true的话,这里可以只判断 isRequest就可以,
isRequest = false;
ArrayList<LatLng> listLatlngs = new ArrayList<LatLng>();
listLatlngs.add(latlng);
goMarkerCenter(mBaiduMap, listLatlngs);
latlng2tv(latlng);
}
//首次定位完成
isFirstLoc = false;
}
@Override
public void onConnectHotSpotMessage(String arg0, int arg