1

I have an application in which i am getting latitude and longitude after opening the map, and then i am showing the nearest place from on that latitude and longitude, Is there any way to get latitude and longitude without opening the map.

 @Override
public void onLocationChanged(Location location) {
    Log.d("onLocationChanged", "entered");

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
    mCurrLocationMarker = mMap.addMarker(markerOptions);

    //set lat and long 

    HomeActivity.lats = String.valueOf(latitude);
    HomeActivity.lngs = String.valueOf(longitude);
    //move map camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(16));
    // Toast.makeText(SearchMasjid.this, "Your Current Location", Toast.LENGTH_LONG).show();

    Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f", latitude, longitude));

    //stop location updates
    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        Log.d("onLocationChanged", "Removing Location Updates");
    }
    Log.d("onLocationChanged", "Exit");
    hidePDialog();

}
  • Yeah it is very possible, check this https://stackoverflow.com/a/17519248/1380032 – Rahul Sep 30 '17 at 17:10
  • getLastKnownLocation is not perfact to get axact location... And that's why I am not using this method. – Shadab Azam Farooqui Sep 30 '17 at 21:02
  • You'll need to request location updates according to the [Android documentation](https://developer.android.com/training/location/receive-location-updates.html) and then remove the updates when you don't need them anymore. – Markus Kauppinen Oct 01 '17 at 07:41

0 Answers0