qq_34316891 2016-06-05 03:52 采纳率: 100%
浏览 1090

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条回答

  • threenewbee 2016-06-05 05:02
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接