2011-09-28 49 views
0

可能重複:
GPS not update location after close and reopen app on android關於當前位置的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()

回答

0

引起它示出了你最後知道的位置,這就是你的代碼呢。

您需要給LocationManager一些時間來查找您當前的位置,您不能期望它在不計算它的情況下知道它。

你可以做的是:

  1. 撥打的AsyncTask。
  2. 在preExecute中創建一個對話框或執行某些操作來顯示用戶正在查找當前位置。
  3. 在backgroundProccess中獲取新位置。
  4. 在postExecute中通知用戶新的(當前)位置。
  5. 當您按下按鈕時,運行AsyncTask。