无法显示定位问题
package com.example.diy_baidumap; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.MapView; public class MainActivity extends AppCompatActivity { private MapView mMapView = null; private BaiduMap mBaiduMap = null; LocationClient mLocationClient = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMapView = findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); } @Override protected void onResume() { mMapView.onResume(); super.onResume(); } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onDestroy() { mLocationClient.stop(); mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; super.onDestroy(); } private final static int ITEM_ID_BAIDUMAP_NORMAL = 101; private final static int ITEM_ID_BAIDUMAP_SATELLITE = 102; private final static int ITEM_ID_BAIDUMAP_SHOW_LOCATION = 103; @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(Menu.NONE, ITEM_ID_BAIDUMAP_NORMAL, 1, "切换为普通地图"); menu.add(Menu.NONE, ITEM_ID_BAIDUMAP_SATELLITE, 2, "切换为卫星地图"); menu.add(Menu.NONE, ITEM_ID_BAIDUMAP_SHOW_LOCATION, 3, "显示定位"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case ITEM_ID_BAIDUMAP_NORMAL: mBaiduMap.setMapType(ITEM_ID_BAIDUMAP_NORMAL); break; case ITEM_ID_BAIDUMAP_SATELLITE: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); break; case ITEM_ID_BAIDUMAP_SHOW_LOCATION: mBaiduMap.setMyLocationEnabled(true); mLocationClient = new LocationClient(this.getApplicationContext()); //通过LocationClientOption设置LocationClient相关参数 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000); //设置locationClientOption mLocationClient.setLocOption(option); //注册LocationListener监听器 MyLocationListener myLocationListener = new MyLocationListener(this.mMapView); mLocationClient.registerLocationListener(myLocationListener); //开启地图定位图层 mLocationClient.start(); break; } return super.onOptionsItemSelected(item); } }
正在回答 回答被采纳积分+1
【补充】
public class MyLocationListener extends BDAbstractLocationListener {
private MapView mMapView;
private BaiduMap mBaiduMap;
public MyLocationListener(MapView mMapView) {
this.mMapView = mMapView;
this.mBaiduMap = mMapView.getMap();
}
@Override
public void onReceiveLocation(BDLocation location) {
//mapView 销毁后不在处理新接收的位置
if (location == null || mMapView == null){
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
}
}
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星