Get current coordinates - android

Im writing an application that supposed to send coordinates in an SMS, but I've been struggling a bit with understanding how to get the coordinates.
At the moment I'm using this
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
And then i pass the long and lat into the text, but this only gives me the last know location i guess?
Can anyone tell me how to get the current location?
Regards
/Fred

you need to create a LocationListener and pass it to the LocationManager like this: locationManager.requestLocationUpdates(
locationManager.getBestProvider(fine, true),
minTime, 0, listenerFine);
You will get your lat/long updates from the listener in onLocationChanged()

Please referred Below Link it may be Help You
Get user current position with gps/network android MapView
or
http://www.vogella.de/articles/AndroidLocationAPI/article.html

Related

Why I am getting old location coordinates in my Android app while trying to find the current location co-ordinates?

I've simply made an app that shows my current location co-ordinates. But if I change my location then it is shows my old location co-ordinates. Again if I reboot my device it shows the right co-ordinates. Here is a part of my code where I got my co-ordinates:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BWN_UPDATE,MIN_DIST_CHANGE_FOR_UPDATES,this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (location != null){
longitude = location.getLongitude();
latitude = location.getLatitude();
}
Use onLocationChanged() callback instead of getLastKnownLocation(). Here is an example code :
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location != null){
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
};
How and when to register Location Listener?
If you want location listener for display current location on map then you should register Location Listener in onCreate() or onResume() method of the activity and unregister receiver in onPause() or onStop() method. To unregister location updates you can use the below method.
locationManager.removeUpdates(locationListener)
Also location lock by GPS is always not possible so I will suggest you to use Network provider too. Just add this statement.
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BWN_UPDATE,MIN_DIST_CHANGE_FOR_UPDATES,locationListener);
and do this change to your GPS registration statement by putting locationListener instead of this as your third argument of locationManager.requestLocationUpdates();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BWN_UPDATE,MIN_DIST_CHANGE_FOR_UPDATES,locationListener);
getLastKnownLocation() isn't good for getting accurate results instantly since it works without waiting for GPS fix - it just gives you location, which is cached by system (you could try running other location using app e.g. Google Maps, then running your and location will be newer).
To make it work as expected you could use onLocationChanged method of LocationListener interface - try one explained here How to get current location in Android
The GPS function takes its time to identify your location on request.
While you are changing you location, you need to wait for time that GPS set new location. Till that time, LocationManager will return your old location in :
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
In Samsung Galaxy devices, when you start GPS (Location) button, the icon start blinking in statusbar area. This is to tell user that GPS is searching new location now. When new location is found then the icon will stop blinking and notification will appear "Location set by GPS" Technically, you will get callback onLocationChanged() at this time. For this callback, you need to implement LocationListener
When you are rebooting device, GPS has got sufficient time to set new location so it is giving you correct location

Android. How to know if location detected is from GPS or Network provider

I'm trying to implement an application which senses position from both GPS and the network provider and shows it on a Google Map. I succeeded in doing this, but I have a question:
I get the provider in the onCreate() of my Activity (it extends LocationListener)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
It works fine, my problem is that when onLocationChange() is called, I should act differently if the provider which called it is GPS or NETWORK. Is there a way to know which?
To be more clear:
When onLocationChanged(Location location) is called, is there a chance to know which provider made the call? I have tried using an if on the provider string but it seems it doesn't work.
Do you need to know the Location provider (GPS, WIFI or Network) or its accuracy?
getAccuracy()
Get the estimated accuracy of this location, in meters.
We define accuracy as the radius of 68% confidence. In other words, if you draw a circle centered at this location's latitude and longitude, and with a radius equal to the accuracy, then there is a 68% probability that the true location is inside the circle.
If you really care about the provider, you could probably use isProviderEnabled().
check that accuracy is <= 30m:
boolean isGPS = (location.getAccuracy() <= 30);
I think you wanna know which provider provided last Location update .....
u have created Provider as a string just use that String in code like
if (provider.matches("GPS")){}
I see several answers, and although they may be useful, none addresses the actual question asked:
When onLocationChanged(Location location) is called, is there a chance to know which provider made the call?
To know whether it was GPS_PROVIDER or NETWORK_PROVIDER the one triggering the onLocationChanged you can use the getProvider() method (or the provider value in Kotlin):
#Override
public void onLocationChanged(Location location){
//obtain a string, 'gps' or 'network', from the location
System.out.println(location.getProvider());
}
Or in Kotlin:
override fun onLocationChanged(location: Location?) {
println(location!!.provider)
}
Doc reference here

Getting longitude and latitude values faster

I am developing an application in android and I want to get the latitude and longitude of the android mobile. My code for getting latitude and longitude is giving values after some time. And sometimes, it doesn't give values at all. Can anyone help me what can be done!
You need to add this code for getting te current location. make sure you give all the permissions
Location Manager location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!=null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Please used last know location from your location service provider if you not get latest because
When your application is not running at that time some other application used location manager to get current location.
then once you start your application with location manager,what happen location manager basically take some time to prepare to send current location so at that time you have to get last know location from location provider,for that you have to design service in such way.
once the location manager is stable then used those location.
this is best way.
Thank you
Bhavdip
One quicker way would be to get the last known location getLastKnownLocation()
and check if the time of this location is not far location.getTime(). Or use it until you get the real location.
Another tip is to use both GPS_PROVIDER and NETWORK_PROVIDER so your code will be like this :
LocationManager locManager;
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
/*call both providers , the quickest one will call the listener and in the listener you remove the locationUpdates*/
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0 ,0 , locationListener);
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0 ,0 , locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
/*check both providers even for lastKnownLocation*/
if (location == null)
{
location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if(location != null) {
String lat = String.valueOf(location.getLatitude());
v1.setText(lat);
String longi = String.valueOf(location.getLatitude());
v2.setText(longi);
}
this is a simple example, a more elaborate solution would compare the newest lastKnownLocation between providers.

Android LocationManager class is not returning correct latitude/longitude

I created a program that displays a mapView, with the location of the phone, and the ability to send this location (latitude, longitude) through email. (to add some places to a database).
I'm using the LocationManager class and getLatitude / getLongitude methods to get the phone's location. My code looks like this:
double MyLongitude = 0;
double MyLatitude = 0;
Location location = null;
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, new ShowLocation());
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
MyLatitude = location.getLatitude();
MyLongitude = location.getLongitude();
When test my application on the emulator, everything is OK: mapView displays the location I fixed (using telnet and geo fix command), and I can send the location through email. I get the same result on the phone of a friend; everything seems to work OK.
The problem is on my phone. I can display my location on the mapView (the little blue circle), but when I try to send my location through email, the Latitude and Longitude were both set to 0.
Here is the code to display my location on the mapView:
MyLocationOverlay myLocation = new MyLocationOverlay(this, mapView);
myLocation.enableMyLocation();
I don't understand why my location appears to work when displayed, but I can't get the correct latitude and longitude, while it seems to work on emulator and my friend's phone.
Is this due to missing code or something like that? Is this a problem with my phone? I checked the permissions, and I already have the needed ones:
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
I also tried to test with LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER, but got the same result.
You can override onLoationChanged(Location l) on your MyLocationOverlay, which will get called whenever the overlay gets a new location to draw your position, instead of requesting location updates and getLastKnownLocation.
I am guessing that getLastKnownLocation is returning 0, 0 because there hasn't been a location fix yet.

How to get altitude in Android

I use the Location class to get altitude, but it always returns 0.
Please tell me how to get altitude?
Location.getAltitude();
Initially I used location.getAltitude() and it always retuned 0.0 . Then I came across https://groups.google.com/forum/#!topic/android-beginners/KNeLh905ip0 it helped me solve my problem. It works on actual real device.
private LocationProvider _locationProvider;
private LocationManager _locationManager;
_locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
_locationProvider = _locationManager
.getProvider(LocationManager.GPS_PROVIDER);
location = _locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
_locationProvider.supportsAltitude();
hasaltitude = location.hasAltitude();
double altitude = location.getAltitude();
System.out.println("HasAltitude" + hasaltitude+"-"+altitude);
The value of "altitude" is will be in meters.
Altitude is typically only available with GPS provider. You can check with available location provider if the support altitude:
locationProvider.hasAltitude();
You need to obtain the user Location :
http://developer.android.com/guide/topics/location/obtaining-user-location.html
The locationListener will give you a Location object, on which you can use the getAltitude() function.
With all the different methods to get location, while request a location for high accuracy it requires permission ACCESS_FINE_LOCATION

Categories

Resources