2017-04-07 34 views
-1

的間隔獲得更新的位置,我想用1分鐘的間隔,以獲得更新的位置對我來說,把標記在地圖上,同時使其他應用程序可以看到我每當我移動時,都會在當前位如何以1分的ANDROID

因此,這裏是我的代碼,但痛心地說即使代碼已經是onLocationChanged方法裏面將不會更新位置。但我不確定如果我的代碼是正確的。

@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

「LocationRequest」的郵編「 –

+0

」https://developers.google.com/android/reference/com/google/android/GMS /位置/ LocationRequest –

回答

1

看起來你在呼喚:

 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 

的onLocationChanged方法內。這將在第一次調用onLocationChanged後立即停止位置更新。

嘗試刪除該行,看看它是否工作。您只能在完成使用位置服務後調用removeLocationUpdates,或者您的應用已暫停/停止

+0

它的工作原理,謝謝! – tin