Search code examples
androidlocationmarkerlocationmanager

How to get the updated location with an interval of 1 minute ANDROID


I want to get the updated location with an interval of 1 minute for me to put the marker on the map and at the same so that the other application can see my current location whenever I move.

So here's my code but sad to say it won't update the location even if the code is already inside the onLocationChanged method. But I'm not sure though if my code's right.

@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);
    }\
}

Solution

  • It looks like you are calling:

            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    

    inside the onLocationChanged method. This will stop location updates immediately after the first call to onLocationChanged.

    Try removing that line and see if it works. You should only call removeLocationUpdates when you are done using location services, or if your app gets paused/stopped