12445

Problem in Getting the Current Location in Android

Question:

I want to display latitude and longitude my current location.. For this rather than searching in Google, i searched in SO.

I just want to display my current location latitude and longitude.

See my code below :

public class LocationGetter extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new LocationManagerHelper(); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener); if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { tv.append("Latitude:- " + LocationManagerHelper.getLatitude() + '\n'); tv.append("Longitude:- " + LocationManagerHelper.getLongitude() + '\n'); Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude())); } else { tv.setText("GPS is not turned on..."); } /** set the content view to the TextView */ setContentView(tv); } /* Class My Location Listener */ public static class LocationManagerHelper implements LocationListener { private static double latitude; private static double longitude; @Override public void onLocationChanged(Location loc) { latitude = loc.getLatitude(); longitude = loc.getLongitude(); Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude)); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } public static double getLatitude() { return latitude; } public static double getLongitude() { return longitude; } } }

I have also added permission in manifest file :

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

See the output i am getting :

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/SqNHO.jpg" data-original="https://i.stack.imgur.com/SqNHO.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Where i am going wrong ? I dont understand, its a simple code and why its not working ?

Answer1:

I just verified your code and it works with these changes, you just need to move the tv.append() calls to the onLocationChanged() method as that is called each time, if you don't use that for the CallBack, then u will get the first set values only and it's only executed once.

public void onLocationChanged(Location loc) { latitude = loc.getLatitude(); longitude = loc.getLongitude(); Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude)); tv.append("Latitude:- " + LocationManagerHelper.getLatitude() + '\n'); tv.append("Longitude:- " + LocationManagerHelper.getLongitude() + '\n'); Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude())); }

and I have used these permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />

You can look at these and filter out the ones you won't be interested in. I have used a downloaded .gpx file from <a href="http://puriri.tramper.co.nz/?view=browsegpxFiles&offset=61" rel="nofollow">here</a>

I've tested it on Android 2.2 though, <img alt="Location Updates working" class="b-lazy" data-src="https://i.stack.imgur.com/M3oth.png" data-original="https://i.stack.imgur.com/M3oth.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer2:

You got location in Onlocationchanged

public void onLocationChanged(Location loc) { latitude = loc.getLatitude(); longitude = loc.getLongitude(); Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude)); }

so the lattitude and longitude will be displayed only when the change from the current location.for checking you give some latitude and longitude and send from your emulator control in DDMS and after that you run your program.Sure it will show the location.

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/Xh0h3.png" data-original="https://i.stack.imgur.com/Xh0h3.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer3:

If you run this program in device then it show latitude and longitude 0.0 until we change our position.You must move at least 10 meter from your current position to see the output. Because without change current position onLocationChange() method does not fire and output will be 0.0

Answer4:

Use the <a href="http://developer.android.com/intl/de/reference/android/location/LocationManager.html" rel="nofollow">LocationManager</a>.

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); double longitude = location.getLongitude(); double latitude = location.getLatitude();

The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a <a href="http://developer.android.com/intl/de/reference/android/location/LocationListener.html" rel="nofollow">LocationListener</a> to the <a href="http://developer.android.com/intl/de/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.app.PendingIntent%29" rel="nofollow">requestLocationUpdates() method</a> instead, which will give you asynchronous updates of your location.

private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { longitude = location.getLongitude(); latitude = location.getLatitude(); } } lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener);

You'll need to give your application the <a href="http://developer.android.com/intl/de/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION" rel="nofollow">ACCESS_FINE_LOCATION permission</a> if you want to use GPS.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

You may also want to add the <a href="http://developer.android.com/intl/de/reference/android/Manifest.permission.html#ACCESS_COARSE_LOCATION" rel="nofollow">ACCESS_COARSE_LOCATION permission</a> for when GPS isn't available and select your location provider with the <a href="http://developer.android.com/intl/de/reference/android/location/LocationManager.html#getBestProvider%28android.location.Criteria,%20boolean%29" rel="nofollow">getBestProvider() method</a>.

Answer5:

I think with the emulator you can't get the GPS co-ordinates. If you are working with the mobile, try the GPS outside of your building. sometimes you won't get the GPS co-ordinates inside the building. If that time also you failed, then i will post the code that we got succeeded.

The below code just now i have tested and its works for me..

import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.Toast; public class GetGPS extends Activity { String GPSPROVIDER = LocationManager.GPS_PROVIDER; private static final long MIN_GEOGRAPHIC_POOLING_TIME_PERIOD = 10000; private static final float MIN_GEOGRAPHIC_POOLING_DISTANCE = (float)5.0; public LocationManager gpsLocationManager; static Context context = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); context = this; /*Get a listener for GPS*/ LocationListener gpsLocationListener = null; gpsLocationListener = new GpsLocationListener(this); /*Start Location Service for GPS*/ gpsLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); /*Register GPS listener with Location Manager*/ gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_GEOGRAPHIC_POOLING_TIME_PERIOD, MIN_GEOGRAPHIC_POOLING_DISTANCE, gpsLocationListener); boolean isavailable = gpsLocationManager.isProviderEnabled(GPSPROVIDER); if(isavailable) { Location loc = gpsLocationManager.getLastKnownLocation("gps"); if(loc != null) { double latitude = loc.getLatitude(); double longitude = loc.getLongitude(); Toast.makeText(GetGPS.this,"Longitude is "+longitude + " Latitude is "+latitude, Toast.LENGTH_LONG).show(); } } } public static class GpsLocationListener implements LocationListener { public GpsLocationListener(Context context){ } public void onLocationChanged(Location loc) { if (loc != null) { double latitude = loc.getLatitude(); double longitude = loc.getLongitude(); Toast.makeText(context,"Longitude is "+longitude + " Latitude is "+latitude, Toast.LENGTH_LONG).show(); } }//End of onLocationChanged Method @Override public void onProviderDisabled(String provider) { }//End of onProviderDisabled Method @Override public void onProviderEnabled(String provider) { }//End of onProviderEnabled Method @Override public void onStatusChanged(String provider, int status, Bundle extras) { }//End of onStatusChanged Method }//End of GpsLocationListener class }

I tested this by moving the device from one place to another...

I hope it will helps..

Recommend