Reputation: 467
I want to determine my curent position (latitude, longitude and altitude). In my database, I have a table contents by table location (id,name,address,longitude,latitude,altitude)
How can I determine my curent position and the most near location?
I Added this in manifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
How can i use the methods (.getlongitude(); .getLatitude(); .getaltitude()
In documentation this is not clear.
Thank you in advance.
Upvotes: 1
Views: 6313
Reputation: 1155
First Enable Ur GPS.
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
public static double lontitube,latitude;
protected LocationManager locationManager;
protected Location currentLocation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
}
protected void performReverseGeocodingInBackground() {
showCurrentLocation();
}
protected void showCurrentLocation() {
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLocation != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
currentLocation.getLongitude(), currentLocation.getLatitude()
);
lontitube=currentLocation.getLongitude();
latitude=currentLocation.getLatitude();
System.out.println("----lati----longi----"+latitude+"\n"+lontitube);
Toast.makeText(Demo.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(Demo.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(Demo.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(Demo.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(Demo.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
In AndroidManifest add
ACCESS_FINE_LOCATION
permission for GPS use.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Upvotes: 3
Reputation: 16874
You can choose to either get the last known location, or listen for updates. The easiest way is to get the latest:
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
If you want an accurate location, you need to listen forupdates using requestLocationUpdates().
Also, remember to check whether the GPS device is enabled, see How do I find out if the GPS of an Android device is enabled
To find the nearest addresses, see http://developer.android.com/reference/android/location/Geocoder.html
Upvotes: 4