2017-04-07 9 views
-1

地図上にマーカーを置くために1分の間隔で更新された位置を取得したいので、他のアプリケーションが私が移動するたびに現在の位置。1分の間隔で更新された位置を取得する方法

これは私のコードですが、コードがすでに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/location/LocationRequest –

答えて

1

呼び出しているように見えます:

 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 

をonLocationChangedメソッド内。これにより、onLocationChangedの最初の呼び出しの直後に位置の更新が停止されます。

その行を削除して、それが機能するかどうかを確認してください。位置情報サービスの使用が完了したとき、またはアプリが一時停止/停止したときにのみremoveLocationUpdatesを呼び出す必要があります。

+0

これは動作します、ありがとうございます! – tin

関連する問題