2013-01-14 53 views
5

我試圖通過gps提供商首先生成一個位置,如果我得到一個空,我通過網絡提供商生成。 我的問題是,網絡提供商生成兩次相同的位置..我發送信息到數據庫,它發送相同的位置相同的時間(相同的秒!)。 下面是一些代碼:網絡提供商生成一個位置的2個副本

gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps); 
networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER, R.string.not_support_gps); 
    // Update the UI immediately if a location is obtained. 
if (gpsLocation != null) 
    updateUILocation(gpsLocation); 
else 
    updateUILocation(networkLocation); 

這裏是requestUpdateFromProvider:

private Location requestUpdatesFromProvider(final String provider, final int errorResId) 
{ 
    Location location = null; 
    if (mLocationManager.isProviderEnabled(provider)) 
    { 
     mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS*2, listener); 
     location = mLocationManager.getLastKnownLocation(provider); 
    } 
    else 
    { 
     Toast.makeText(this, errorResId, Toast.LENGTH_LONG).show(); 
    } 
    return location; 
} 

這裏是updateUILocation:

private void updateUILocation(Location location) 
{ 
    // We're sending the update to a handler which then updates the UI with the new location. 
    Message.obtain(mHandler, 
      UPDATE_LATLNG, 
      location.getLatitude() + ", " + location.getLongitude()).sendToTarget(); 

    // Bypass reverse-geocoding only if the Geocoder service is available on the device. 
    if (mGeocoderAvailable) 
     doReverseGeocoding(location); 
} 

這裏是處理:

mHandler = new Handler() 
    { 
     public void handleMessage(Message msg) 
     { 
      if(msg.what == UPDATE_ADDRESS) //UPDATE_ADDRESS = 1 
      { 
       LocationService.this.address = (String) msg.obj; // update a string that show the user address 
       new SendLocation(LocationService.this.id,(String)msg.obj);//send the info to the db. 
      } 
     } 
    }; 

SendLocation正在做正確的操作,所以你不應該注意它。

EDIT

我忘了說,代碼的第一塊是從構造調用的方法。

回答

0

如果它不是你的錯,創建了重複的位置,那麼只需調用一個同步的forwardLocation(Location loc)方法,即存儲上次接收位置的時間戳。 當上一次存儲的時間戳等於前一個時,這個方法應該忽略一個位置。

相關問題