Android中百度地图定位问题

public class MainActivity extends Activity {
private MapView mapView;
private LocationManager locationManager;
private String provider;
private Location location;
private BaiduMap baiduMap;
private boolean isFirstLocate=true;
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    SDKInitializer.initialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    mapView=(MapView)findViewById(R.id.map1_view);
    baiduMap=mapView.getMap();
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    List<String>  providerList=locationManager.getProviders(true);
    if(providerList.contains(LocationManager.GPS_PROVIDER)){
        provider=LocationManager.GPS_PROVIDER;
    }else if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
        provider=LocationManager.NETWORK_PROVIDER;
    }else{
        Toast.makeText(this, "请打开GPS或者联网!", Toast.LENGTH_LONG).show();
        return;
    }

    location=locationManager.getLastKnownLocation(provider);

    if(location !=null){
        Log.d("map", "ffff");
        showLocation(location);
    }
    locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
}

LocationListener locationListener=new LocationListener() {

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location arg0) {
    if(location!=null){
        showLocation(location);
    }

}

};
protected void showLocation(Location location){

    if(isFirstLocate){
    LatLng latLng=new LatLng(location.getLatitude(), location.getAltitude());
    MapStatusUpdate update=MapStatusUpdateFactory.newLatLng(latLng);
    baiduMap.animateMapStatus(update);
    update=MapStatusUpdateFactory.zoomTo(16f);
    baiduMap.animateMapStatus(update);
    isFirstLocate=false;
    }
    Log.d("map", "ggggg");
}
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    mapView.onDestroy();
    if(location!=null){
        locationManager.removeUpdates(locationListener);
    }
}

为什么我一直无法地位到我所在地,还有就是我的key什么都没错,因为我地图能显示默认的北京地图。

1个回答

下个断点看下,因为gps坐标是异步获取的,看下是不是没有获取到你的gps坐标。

qq_34316891
qq_34316891 然而我联网用数据也不行啊。
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐