本人写的代码自本人定位的位置是正确的,但是地图显示的是网格,代码如下,求求各位同仁帮助解答,
public class FindChildMapActivity extends Activity {
private Toast mToast;
private BMapManager mBMapManager;
private MapView mMapView;
private MapController mMapController = null;
private Button request;
/**
* 定位SDK的核心类
*
*/
private LocationClient mLocClient;
/**
* 用户位置信息
*/
private LocationData mLocData;
/**
* 本人的位置图层
*/
private LocationOverlay myLocationOverlay = null;
/**
* 弹出窗口图层
*/
private PopupOverlay mPopupOverlay = null;
private boolean isRequest = false;// 能否手动触发请求定位
private boolean isFirstLoc = true;// 能否首次定位
/**
* 弹出窗口图层的View
*/
private View mPopupView;
private BDLocation location;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 使用地图sdk前需先初始化BMapManager,这个必须在setContentView()先初始化
System.out.println(“44444444444444444444444444444444”);
mBMapManager = new BMapManager(this.getApplicationContext());
// 第一个参数是API key,
// 第二个参数是常用事件监听,用来处理通常的网络错误,授权验证错误等,你也可以不添加这个回调接口
mBMapManager.init(“xiKjZXfbkjqX8ivUV90OwZVu”,
new MKGeneralListenerImpl());
setContentView(R.layout.maplayout);
// 点击按钮手动请求定位
((Button) findViewById(R.id.request))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
requestLocation();
}
});
mMapView = (MapView) findViewById(R.id.bmapView);// 获取百度地图控件实例
mMapController = mMapView.getController();// 获取地图控制器
mMapController.enableClick(true);// 设置地图能否响应点击事件
mMapController.setZoom(14);// 设置地图缩放级别
mMapView.setBuiltInZoomControls(true);// 显示内置缩放控件
// mMapView.setSatellite(true); //设置卫星图
// mMapController.setOverlooking(-45); //设置地图俯视角度 ,范围:0~ -45
mPopupView = LayoutInflater.from(this).inflate(R.layout.pop_layout,
null);
mPopupOverlay = new PopupOverlay(mMapView, new PopupClickListener() {
/**
* 点击弹出窗口图层回调的方法
*/
@Override
public void onClickedPopup(int arg0) {
// TODO Auto-generated method stub
// 隐藏弹出窗口图层
mPopupOverlay.hidePop();
}
});
mLocData = new LocationData();
// 实例化定位服务,LocationClient类必须在主线程中声明
mLocClient = new LocationClient(getApplicationContext());
mLocClient.registerLocationListener(new BDLocationListenerImpl());// 注册定位监听接口
int a = mLocClient.requestLocation();
System.out.println(“a=” + a + ” –返回数据”);
/**
* 设置定位参数
*/
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开GPS
option.setAddrType(“all”);// 返回的定位结果包含地址信息
option.setCoorType(“bd09ll”);// 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000); // 设置发起定位请求的间隔时间为5000ms
option.disableCache(false);// 禁止启用缓存定位
// option.setPoiNumber(5); //最多返回POI个数
// option.setPoiDistance(1000); //poi查询距离
// option.setPoiExtraInfo(true); //能否需要POI的电话和地址等详细信息
mLocClient.setLocOption(option);
mLocClient.start(); // 调用此方法开始定位
// 定位图层初始化
myLocationOverlay = new LocationOverlay(mMapView);
myLocationOverlay.setData(mLocData);
myLocationOverlay.setMarker(getResources().getDrawable(
R.drawable.location_arrows));
// 添加定位图层
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
// 修改定位数据后刷新图层生效
mMapView.refresh();
}
/**
* 定位接口,需要实现两个方法
*
* @author xiaanming
*
*/
public class BDLocationListenerImpl implements BDLocationListener {
/**
* 接收异步返回的定位结果,参数是BDLocation类型参数
*/
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (location == null) {
return;
}
FindChildMapActivity.this.location = location;
mLocData.latitude = location.getLatitude();
mLocData.longitude = location.getLongitude();
// 假如不显示定位精度圈,将accuracy赋值为0即可
mLocData.accuracy = location.getRadius();
mLocData.direction = location.getDerect();
// 将定位数据设置到定位图层里
myLocationOverlay.setData(mLocData);
// 更新图层数据执行刷新后生效
mMapView.refresh();
if (isFirstLoc || isRequest) {
System.out.println(“本人来了………………………………111111111111111111111111”);
mMapController.animateTo(new GeoPoint((int) (location
.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6)));
showPopupOverlay(location);
isRequest = false;
}
isFirstLoc = false;
}
/**
* 接收异步返回的POI查询结果,参数是BDLocation类型参数
*/
@Override
public void onReceivePoi(BDLocation arg0) {
// TODO Auto-generated method stub
}
}
/**
* 常用事件监听,用来处理通常的网络错误,授权验证错误等
*
* @author xiaanming
*
*/
public class MKGeneralListenerImpl implements MKGeneralListener {
/**
* 一些网络状态的错误处理回调函数
*/
@Override
public void onGetNetworkState(int iError) {
// TODO Auto-generated method stub
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
showToast(“你的网络出错了”);
}
}
/**
* 授权错误的时候调用的回调函数
*/
@Override
public void onGetPermissionState(int iError) {
// TODO Auto-generated method stub
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
showToast(“API KEY错误, 请检查!”);
}
}
}
// 继承MyLocationOverlay重写dispatchTap方法
private class LocationOverlay extends MyLocationOverlay {
public LocationOverlay(com.baidu.mapapi.map.MapView arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
/**
* 在“本人的位置”坐标上处理点击事件。
*/
@Override
protected boolean dispatchTap() {
// TODO Auto-generated method stub
showPopupOverlay(location);
return super.dispatchTap();
}
@Override
public void setMarker(Drawable arg0) {
super.setMarker(arg0);
}
}
/**
* 显示弹出窗口图层PopupOverlay
*
* @param location
*/
private void showPopupOverlay(BDLocation location) {
TextView popText = (TextView) mPopupView
.findViewById(R.id.location_tips);
popText.setText(“[本人的位置]\n” + location.getAddrStr());
mPopupOverlay.showPopup(getBitmapFromView(popText),
new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6)), 10);
}
/**
* 手动请求定位的方法
*/
public void requestLocation() {
isRequest = true;
if (mLocClient != null && mLocClient.isStarted()) {
showToast(“正在定位……”);
mLocClient.requestLocation();
} else {
Log.d(“log”, “locClient is null or not started”);
}
}
/**
* 显示Toast消息吧
*
* @param msg
*/
private void showToast(String msg) {
if (mToast == null) {
mToast = Toast.makeText(FindChildMapActivity.this, msg,
Toast.LENGTH_SHORT);
} else {
mToast.setText(msg);
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
/**
* 将View转换成Bitmap的方法
*
* @param view
* @return
*/
public static Bitmap getBitmapFromView(View view) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
@Override
protected void onResume() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
mMapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
mMapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
// MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
mMapView.destroy();
// 退出应用调用BMapManager的destroy()方法
if (mBMapManager != null) {
mBMapManager.destroy();
mBMapManager = null;
}
// 退出时销毁定位
if (mLocClient != null) {
mLocClient.stop();
}
super.onDestroy();
}
}
public class FindChildMapActivity extends Activity {
private Toast mToast;
private BMapManager mBMapManager;
private MapView mMapView;
private MapController mMapController = null;
private Button request;
/**
* 定位SDK的核心类
*
*/
private LocationClient mLocClient;
/**
* 用户位置信息
*/
private LocationData mLocData;
/**
* 本人的位置图层
*/
private LocationOverlay myLocationOverlay = null;
/**
* 弹出窗口图层
*/
private PopupOverlay mPopupOverlay = null;
private boolean isRequest = false;// 能否手动触发请求定位
private boolean isFirstLoc = true;// 能否首次定位
/**
* 弹出窗口图层的View
*/
private View mPopupView;
private BDLocation location;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 使用地图sdk前需先初始化BMapManager,这个必须在setContentView()先初始化
System.out.println(“44444444444444444444444444444444”);
mBMapManager = new BMapManager(this.getApplicationContext());
// 第一个参数是API key,
// 第二个参数是常用事件监听,用来处理通常的网络错误,授权验证错误等,你也可以不添加这个回调接口
mBMapManager.init(“xiKjZXfbkjqX8ivUV90OwZVu”,
new MKGeneralListenerImpl());
setContentView(R.layout.maplayout);
// 点击按钮手动请求定位
((Button) findViewById(R.id.request))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
requestLocation();
}
});
mMapView = (MapView) findViewById(R.id.bmapView);// 获取百度地图控件实例
mMapController = mMapView.getController();// 获取地图控制器
mMapController.enableClick(true);// 设置地图能否响应点击事件
mMapController.setZoom(14);// 设置地图缩放级别
mMapView.setBuiltInZoomControls(true);// 显示内置缩放控件
// mMapView.setSatellite(true); //设置卫星图
// mMapController.setOverlooking(-45); //设置地图俯视角度 ,范围:0~ -45
mPopupView = LayoutInflater.from(this).inflate(R.layout.pop_layout,
null);
mPopupOverlay = new PopupOverlay(mMapView, new PopupClickListener() {
/**
* 点击弹出窗口图层回调的方法
*/
@Override
public void onClickedPopup(int arg0) {
// TODO Auto-generated method stub
// 隐藏弹出窗口图层
mPopupOverlay.hidePop();
}
});
mLocData = new LocationData();
// 实例化定位服务,LocationClient类必须在主线程中声明
mLocClient = new LocationClient(getApplicationContext());
mLocClient.registerLocationListener(new BDLocationListenerImpl());// 注册定位监听接口
int a = mLocClient.requestLocation();
System.out.println(“a=” + a + ” –返回数据”);
/**
* 设置定位参数
*/
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开GPS
option.setAddrType(“all”);// 返回的定位结果包含地址信息
option.setCoorType(“bd09ll”);// 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000); // 设置发起定位请求的间隔时间为5000ms
option.disableCache(false);// 禁止启用缓存定位
// option.setPoiNumber(5); //最多返回POI个数
// option.setPoiDistance(1000); //poi查询距离
// option.setPoiExtraInfo(true); //能否需要POI的电话和地址等详细信息
mLocClient.setLocOption(option);
mLocClient.start(); // 调用此方法开始定位
// 定位图层初始化
myLocationOverlay = new LocationOverlay(mMapView);
myLocationOverlay.setData(mLocData);
myLocationOverlay.setMarker(getResources().getDrawable(
R.drawable.location_arrows));
// 添加定位图层
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
// 修改定位数据后刷新图层生效
mMapView.refresh();
}
/**
* 定位接口,需要实现两个方法
*
* @author xiaanming
*
*/
public class BDLocationListenerImpl implements BDLocationListener {
/**
* 接收异步返回的定位结果,参数是BDLocation类型参数
*/
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (location == null) {
return;
}
FindChildMapActivity.this.location = location;
mLocData.latitude = location.getLatitude();
mLocData.longitude = location.getLongitude();
// 假如不显示定位精度圈,将accuracy赋值为0即可
mLocData.accuracy = location.getRadius();
mLocData.direction = location.getDerect();
// 将定位数据设置到定位图层里
myLocationOverlay.setData(mLocData);
// 更新图层数据执行刷新后生效
mMapView.refresh();
if (isFirstLoc || isRequest) {
System.out.println(“本人来了………………………………111111111111111111111111”);
mMapController.animateTo(new GeoPoint((int) (location
.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6)));
showPopupOverlay(location);
isRequest = false;
}
isFirstLoc = false;
}
/**
* 接收异步返回的POI查询结果,参数是BDLocation类型参数
*/
@Override
public void onReceivePoi(BDLocation arg0) {
// TODO Auto-generated method stub
}
}
/**
* 常用事件监听,用来处理通常的网络错误,授权验证错误等
*
* @author xiaanming
*
*/
public class MKGeneralListenerImpl implements MKGeneralListener {
/**
* 一些网络状态的错误处理回调函数
*/
@Override
public void onGetNetworkState(int iError) {
// TODO Auto-generated method stub
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
showToast(“你的网络出错了”);
}
}
/**
* 授权错误的时候调用的回调函数
*/
@Override
public void onGetPermissionState(int iError) {
// TODO Auto-generated method stub
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
showToast(“API KEY错误, 请检查!”);
}
}
}
// 继承MyLocationOverlay重写dispatchTap方法
private class LocationOverlay extends MyLocationOverlay {
public LocationOverlay(com.baidu.mapapi.map.MapView arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
/**
* 在“本人的位置”坐标上处理点击事件。
*/
@Override
protected boolean dispatchTap() {
// TODO Auto-generated method stub
showPopupOverlay(location);
return super.dispatchTap();
}
@Override
public void setMarker(Drawable arg0) {
super.setMarker(arg0);
}
}
/**
* 显示弹出窗口图层PopupOverlay
*
* @param location
*/
private void showPopupOverlay(BDLocation location) {
TextView popText = (TextView) mPopupView
.findViewById(R.id.location_tips);
popText.setText(“[本人的位置]\n” + location.getAddrStr());
mPopupOverlay.showPopup(getBitmapFromView(popText),
new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6)), 10);
}
/**
* 手动请求定位的方法
*/
public void requestLocation() {
isRequest = true;
if (mLocClient != null && mLocClient.isStarted()) {
showToast(“正在定位……”);
mLocClient.requestLocation();
} else {
Log.d(“log”, “locClient is null or not started”);
}
}
/**
* 显示Toast消息吧
*
* @param msg
*/
private void showToast(String msg) {
if (mToast == null) {
mToast = Toast.makeText(FindChildMapActivity.this, msg,
Toast.LENGTH_SHORT);
} else {
mToast.setText(msg);
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
/**
* 将View转换成Bitmap的方法
*
* @param view
* @return
*/
public static Bitmap getBitmapFromView(View view) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
@Override
protected void onResume() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
mMapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
mMapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
// MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
mMapView.destroy();
// 退出应用调用BMapManager的destroy()方法
if (mBMapManager != null) {
mBMapManager.destroy();
mBMapManager = null;
}
// 退出时销毁定位
if (mLocClient != null) {
mLocClient.stop();
}
super.onDestroy();
}
}
解决方案