Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Latitude and Longitude of the mobile device in android?

How do I get the current Latitude and Longitude of the mobile device in android using location tools?

like image 291
deepthi Avatar asked Feb 09 '10 06:02

deepthi


People also ask

Can I get longitude and latitude on my phone?

Android: Open Google Maps; it will zoom to your approximate location. Press and hold on the screen to drop a pin marker. Click on the dropped pin; latitude and longitude will be displayed below the map.

How to get current location latitude and longitude in Android?

How to get current location latitude and longitude in Android? This example demonstrates how do I get current location latitude and longitude in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to find latitude and longitude in the GPS activity?

– You will start on the MainActivity which is the activity that app will launch initially. – Touch/Click on to ‘Start’ button which will take you to the GPS activity. – After 7-8 seconds you will see Latitude and Longitude on the EditText fields.

How do I Find my GPS location on my Android device?

There are a number of ways to find your GPS location on your Android device. Unlike the iPhone, the Android system doesn’t have a default, built-in GPS coordinate utility that shows you the information the phone already has. You have to find an Android app that can provide this functionality.

Does Android have GPS coordinates?

Unlike the iPhone, the Android system doesn’t have a default, built-in GPS coordinate utility that shows you the information the phone already has. You have to find an Android app that can provide this functionality.


1 Answers

Use the LocationManager.

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 LocationListener to the requestLocationUpdates() method 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_PROVIDER, 2000, 10, locationListener); 

You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.

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

You may also want to add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method.

like image 115
Dave Webb Avatar answered Oct 19 '22 00:10

Dave Webb