qq_34316891
qq_34316891
采纳率50%
2016-06-05 03:52 阅读 1.1k

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条回答 默认 最新

  • caozhy 从今以后生命中的每一秒都属于我爱的人 2016-06-05 05:02

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

    点赞 评论 复制链接分享

相关推荐