0

Good Saturday!

I have a MainActivity class and Location class (actually empty). I want to get device latitude and longitude, so in that way call them in the main activity just like:

Location location new Location();

location.getLatitude();
location.getLongitude();

I don't know if is that possible, I mean do all the work in the Location class and just get the results in the MainActivity (preferably in a number value)... Is that possible? Can somebody please help me?

Greetings!

  • Checkout the answer seems to be in this post: http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android – WizzKidd Nov 19 '16 at 22:25
  • Your answer seems to be in this post: http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android – WizzKidd Nov 19 '16 at 22:34

2 Answers2

0

Are you familiar with: The Google Maps Geolocation API.

Probably you should use the LocationManager.

I think that one of the most useful method for you will be the getLastKnownLocation but that is up to you.

Good luck.

Rotem
  • 1,381
  • 1
  • 11
  • 23
0

1.in Manifest request Internet and location permissions

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

2. make your activity implements LocationListener
3. in on Create method:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

4.// NETWORK_PROVIDER for internet or GPS_PROVIDER for GPS or PASSIVE_PROVIDER for cellular

String provider = LocationManager.NETWORK_PROVIDER; 

5. // 10 meters radius // 5000 milliseconds to update // this - your-activity

locationManager.requestLocationUpdates(provider, 5000, 10, this);

6. in your Implement method:

onLocationChanged(Location location){
location.getLatitude()
location.getLongitude()    
}
Sami Hashwe
  • 24
  • 1
  • 1
  • 9