Android开发中百度地图的定位为什么总是北京,不是自己的位置

如题所述

第1个回答  2017-04-20
给,对照一下
可能的问题:1.用虚拟机,虚拟机定位就是在北京。
2.手机(或虚拟机)是否开启了GPS功能

------------------------------
public class MainActivity extends AppCompatActivity {

MapView mMapView = null;
private BaiduMap mBaiDuMap;
public LocationClient mLocationClient;
// public BDLocationListener myListener = new MyLocationListener();
public MyLocationListener myListener = new MyLocationListener();
private BitmapDescriptor mCurrentMarker;
private MyLocationConfiguration.LocationMode mCurrentMode;

// 经纬度
private TextView tvJingDu;
// 纬度
private TextView tvWeiDu;
// 高度
private TextView tvGaoDu;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);

// 经纬度TextView 初始化
tvJingDu = (TextView) findViewById(R.id.tv_jing_du);
tvWeiDu = (TextView) findViewById(R.id.tv_wei_du);
tvGaoDu = (TextView) findViewById(R.id.tv_gao_du);

//===================================
// 普通模式
mCurrentMode = MyLocationConfiguration.LocationMode.COMPASS;

// TODO
// mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.dingwei);
mCurrentMarker = null;

// 找到地图控件
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiDuMap = mMapView.getMap();// 获取地图

mBaiDuMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);// 设置地图模式为普通模式

// 开启定位图层
mBaiDuMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(this); // 定位用到一个类
mLocationClient.registerLocationListener(myListener);// 注册监听

// TODO
mBaiDuMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker, R.color.myBlue, Color.YELLOW
));

// LocationClientOption类用来设置SDK的位置方式
LocationClientOption option = new LocationClientOption();//以下是给定位设置参数
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(2000);
mLocationClient.setLocOption(option);
mLocationClient.start();

}

boolean isFirstLoc = true; // 是否首次定位

// 3.百度位置监听
public class MyLocationListener implements BDLocationListener {

@Override
public void onReceiveLocation(BDLocation location) {
Log.i("监听被执行了", "监听被执行了");

if (location == null || mMapView == null) {
return;
}

// // 定位结果
// StringBuffer sb = new StringBuffer(256);
// StringBuffer sb1 = new StringBuffer(256);
// StringBuffer sb2 = new StringBuffer(256);
// // 经度
// String jd = sb.append(location.getLongitude()).toString();
// tvJingDu.setText("" + jd);
// Log.i("经度", "" + jd);
// // 纬度
// String wd = sb1.append(location.getLatitude()).toString();
// tvWeiDu.setText("" + sb1);
// Log.i("纬度", "" + wd);
//
// String gaodu = sb.append(location.getAltitude()).toString();
// tvGaoDu.setText("" + gaodu);
// 经度
double d1 = location.getLongitude();
String s1 = "" + d1;
tvWeiDu.setText(s1);
Log.i("经度", "" + s1);
// 纬度
double d2 = location.getLatitude();
Log.i("TGA", "d2" + d2);
String s2 = "" + d2;
tvJingDu.setText(s2);
Log.i("纬度", "" + s2);

MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(0).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiDuMap.setMyLocationData(locData);

if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
// 缩放的等级 12.0f
builder.target(ll).zoom(18.0f);
mBaiDuMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
Log.i("MyLocationListener被执行了!", "onReceiveLocation");

}

@Override
public void onConnectHotSpotMessage(String s, int i) {

}
}

// 结束方法

@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}

@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}

@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
}
第2个回答  2018-10-15
CameraUpdate cameraSigma = CameraUpdateFactory.newCameraPosition(new CameraPosition( new LatLng(39.977290,116.337000), //新的中心点坐标 19, //新的缩放级别 45f, //俯仰角 0~45° (垂直地图时为0) 45f)); //偏航角 0~360° (正北方为0)//移动地图tencentMap.moveCamera(cameraSigma);
第3个回答  2017-04-19
手机所在位置网络信号不好,无法获取你的位置信息
第4个回答  2017-04-19
那个是初始化的默认位置
相似回答