2017-04-07 4 views
-1

Je veux obtenir l'emplacement mis à jour avec un intervalle de 1 minute pour moi de mettre le marqueur sur la carte et en même temps de sorte que l'autre application peut voir mon emplacement actuel chaque fois que je déménage.Comment obtenir l'emplacement mis à jour avec un intervalle de 1 minute ANDROID

Donc voici mon code mais triste de dire qu'il ne mettra pas à jour l'emplacement même si le code est déjà dans la méthode onLocationChanged. Mais je ne suis pas sûr si mon code est correct.

@Override 
public void onLocationChanged(Location location) { 

    mLastLocation = location; 
    if (mCurrLocationMarker != null) { 
     mCurrLocationMarker.remove(); 
    } 
    latlng = new LatLng(location.getLatitude(), location.getLongitude()); 
    lat = String.valueOf(location.getLatitude()); 
    lLong = String.valueOf(location.getLongitude()); 

    driverID = pref.getString("driverID", ""); 

    final Query userName = ref.orderByChild("username").equalTo(username); 
    userName.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot snapShot : dataSnapshot.getChildren()) { 
       if (!snapShot.child("latitude").equals(lat) || !snapShot.child("longitude").equals(lLong)) { 
        snapShot.getRef().child("latitude").setValue(lat); 
        snapShot.getRef().child("longitude").setValue(lLong); 
        snapShot.getRef().child("currentLocation").setValue(strOrigin); 
        Toast.makeText(MapsActivity.this, "old lat: " + snapShot.child("latitude").getValue().toString() + "\nnew lat: " + lat + 
          "\nold long: " + snapShot.child("longitude").getValue().toString() + "\nnew long: " + lLong, Toast.LENGTH_LONG).show(); 
       } 
      } 
      markerOptions = new MarkerOptions(); 
      markerOptions.position(latlng); 
      markerOptions.title("Current Location"); 
      markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
      mCurrLocationMarker = mMap.addMarker(markerOptions); 

      //move map camera 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng)); 
      mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 
     } 

     @Override 
     public void onCancelled(FirebaseError firebaseError) { 
      Toast.makeText(MapsActivity.this, "on cancelled child latlng: " + firebaseError.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    Geocoder geocoder; 
    List<android.location.Address> addresses; 
    geocoder = new Geocoder(this, Locale.getDefault()); 

    StringBuilder result = new StringBuilder(); 

    try { 
     addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); 

     if (addresses.size() > 0) { 
      Address address = addresses.get(0); 
      result.append(address.getAddressLine(0)).append(", "); 
      result.append(address.getLocality()).append(", "); 
      result.append(address.getCountryName()); 
     } 

     strOrigin = result.toString(); 
     Toast.makeText(MapsActivity.this, "origin" + strOrigin, Toast.LENGTH_SHORT).show(); 
     // onSendOrigin(r); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    //stop location updates 
    if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    }\ 
} 
+0

le code postal de 'LocationRequest' –

+0

https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest –

Répondre

1

On dirait que vous appelez:

 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 

dans la méthode OnLocationChanged. Cela arrêtera les mises à jour de localisation immédiatement après le premier appel à onLocationChanged. Essayez de supprimer cette ligne et voir si cela fonctionne.

Vous ne devez appeler removeLocationUpdates que lorsque vous avez fini d'utiliser les services de localisation, ou si votre application est mise en pause/arrêtée

+0

Cela fonctionne, merci! – tin