【问题标题】:Regarding current location in android [duplicate]关于android中的当前位置[重复]
【发布时间】:2011-09-28 15:51:55
【问题描述】:

可能重复:
GPS not update location after close and reopen app on android

我知道这个问题已经被问过了,但我的问题是。

每次我打开我的应用时,它都会显示最后一个位置而不是当前位置

我有一个用于当前位置的按钮,我希望当我打开我的应用程序时它会在地图上显示当前位置,然后当我单击按钮时它会显示当前位置。

所以我的问题是当我打开我的应用程序时它会显示最后一个位置,但是当我按下它时它会显示当前位置。

onCreate() 中的代码

_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    _locationListener = new CurrentLocationListener(this,this);
    _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,_locationListener );
    _currentGeoPoint = getKnownLocation();
    _currentPosOverlay = new OverlayItem(_currentGeoPoint, "", "");
    Log.d(" overlay", "overlaylast "+_currentGeoPoint);
  //  Toast.makeText(getBaseContext(), "overlay12333vvv "+_currentGeoPoint, Toast.LENGTH_SHORT).show();
    _customOverlay.addOverlay(_currentPosOverlay);
    _mapOverlays.add(_customOverlay);

getKnownLocation() 的代码

    // TODO Auto-generated method stub
    Location lastLocation = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);


    if(lastLocation == null){
        lastLocation = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }
    if(lastLocation != null){

        GeoPoint gp = new GeoPoint((int)(lastLocation.getLatitude() * 1e6), (int)(lastLocation.getLongitude() * 1e6));
        last_loc=gp;

        return gp;
    }else {
        return _mapView.getMapCenter();
    }

对于我调用 getKnownLocation() 的按钮

【问题讨论】:

    标签: android gps


    【解决方案1】:

    当然,它会显示您最后知道的位置,这就是您的代码所做的。

    你需要给 LocationManager 一些时间来找到你当前的位置,你不能指望它不计算就知道它。

    你可以做的是:

    1. 创建一个 AsyncTask。
    2. 在 preExecute 中创建一个对话框或执行一些操作以向用户显示您正在寻找当前位置。
    3. 在后台进程中获取一个新位置。
    4. 在 postExecute 中通知用户新的(当前)位置。
    5. 按下按钮时运行 AsyncTask。

    【讨论】:

      猜你喜欢
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2012-10-26
      相关资源
      最近更新 更多