2013-01-14 7 views
5

Sto provando a generare una posizione prima attraverso il provider gps se ottengo un null che sto generando attraverso il provider di rete. Il mio problema è che il provider di rete genera la stessa posizione due volte. Invio le informazioni al db e invia la stessa posizione con la stessa ora (stessi secondi!). Ecco il codice:Il provider di rete genera 2 copie di una posizione

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); 

Ecco la 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; 
} 

Ecco l'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); 
} 

Ecco l'handler:

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 sta facendo ciò che suppone di fare correttamente, quindi non dovresti prestare attenzione ad esso.

EDIT

ho dimenticato di dire che il primo pezzo di codice è un metodo chiamato dal costruttore.

risposta

0

Se non è colpa tua se vengono create posizioni di dupplicate, basta chiamare un metodo sincronizzato forwardLocation(Location loc), che ha memorizzato il timestamp dell'ultima posizione ricevuta. Questo metodo deve ignorare un luogo quando il suo ultimo timestamp memorizzato è uguale al precedente.