2017-04-07 2 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/location/LocationRequest –

답변

1

같습니다 다음 onLocationChanged 방법 내부

 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 

합니다. 그러면 onLocationChanged를 처음 호출 한 직후 위치 업데이트가 중지됩니다.

해당 줄을 제거하고 작동하는지보십시오. 위치 서비스를 사용하거나 앱이 일시 중지/중지 된 경우에만 removeLocationUpdates를 호출해야합니다.

+0

작동합니다. 고맙습니다! – tin

관련 문제