Getting the current GPS location on Android

I am trying to get the current location of the user using the GPS capabilities,

Wrote a simple class that implements LocationListener

 public class LocationManagerHelper implements LocationListener { private static double latitude; private static double longitude; @Override public void onLocationChanged(Location loc) { latitude = loc.getLatitude(); longitude = loc.getLongitude(); } @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; } } 

and from a simple action I get access to these values ​​of longitude and latitude

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** create a TextView and write Hello World! */ TextView tv = new TextView(this); LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new LocationManagerHelper(); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { tv.append("Latitude:- " + LocationManagerHelper.getLatitude() + '\n'); tv.append("Longitude:- " + LocationManagerHelper.getLongitude() + '\n'); } else { tv.setText("GPS is not turned on..."); } /** set the content view to the TextView */ setContentView(tv); 

But it always returns 0.0 as a result. Failed to find out the problem.

+4
source share
5 answers

Location update callbacks cannot be run until the onCreate () function returns. If you initialize your lat / long variables for dummy values, you will probably see that you are printing these values.

Put some entries in your onLocationChanged so you can see that they have been fired, and then think a bit about how Android apps work with callbacks and updating the user interface.

Also, make sure your application has the appropriate permissions in its manifest.

+3
source

Location updates are actually asynchronous. This means that the API does not make your calling thread until a new location is available; instead, you register an observer with a specific method (callback) that is called whenever a new location is computed.

In the Android LocationManager API, the observer is a LocationListener , and the main callback for location updates is onLocationChanged ()

Here is a chart trying to explain this point (hope this helps, not confuse you!)

Sequence diagram

So, from your current code:

  • Declare mlocListener as a member of your Activity subclass
  • Add log outputs (Logcat lines) in your LocationListener implementation
  • Keep the rest of the code as is.
  • Add the correct permissions to the manifest ( FINE_LOCATION is required for GPS), if not already done.
  • Try connecting your phone to the Internet and near the window to get a fairly quick GPS solution (should be ~ 30 seconds).

Then run the application and see what happens in logcat. You will see that status changes and location updates are not immediately after the initial request, explaining why your text always shows (0.0,0.0).

Optional: http://developer.android.com/guide/topics/location/obtaining-user-location.html

+18
source

After you receive mLocListener - set the criteria as shown below

String mlocProvider;
Criteria hdCrit = new Criteria();
hdCrit.setAccuracy(Criteria.ACCURACY_COARSE);
mlocProvider = mlocManager.getBestProvider(hdCrit, true);

and then use getLastKnownLocation

tv.append("\n\nLocations (starting with last known):");
Location currentLocation = mlocManager.getLastKnownLocation(mlocProvider);

Make sure you have it in your manifest
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"

If you use the emulator - in DDMS Perspective, find the location controls on the "Emulator Management" tab. Then use the “Manual” tab to set the longitude and latitude, and click “Submit” - do this when your program is running, you see the onLocationchanged call. It is a good idea to enter onLocationChanged.

BTW, the parameters in requestLocationUpdates are set to "... 0,0 ..." - it will drain your battery - I saw the phone go out within 6 - 8 hours - change it to "... 30000, 100 ... "is the first parameter in milliseconds and the other is in meters.

+2
source

When you test it on the emulator, it will give 0.0. You must transfer the places to the emulator. you can transfer this from the file C: \ android-sdk_r06 \ android-sdk-windows \ tools \ ddms.bat.

There is a tab in ddms called emulator controls. from there you can transfer places to the emulator. try it. hope it works.

0
source

how to create an application?

Getting current location coordinates by action (get location) Lat / long

Send the location to the web server that will be displayed on the map. (Send) Clear the button to clear the old location (Clear Location) or enable (send), it becomes clear (0.0.0.0)

Getting the current GPS location on Android http://developer.android.com/guide/topics/location/obtaining-user-location.html

-3
source

Source: https://habr.com/ru/post/1338527/


All Articles