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.
Related
I am so interested, can i get gps coordinat in offline mode (without internet connection). Is it possible? how can do that?
thanks in advance
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location==null){
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
This will get lat and long from Network provider.
First it will try to get location from GPS. If gps is off, it will return as null. Thats why I put if(location==null) condition.
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
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
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.
I am usin location service for getting latitude and longitude of my location.While running my program it will give me wrong latitude and longitude.How can i get my current location details accurately? Is there any settings in mobile /device ?
String locationProvider = LocationManager.GPS_PROVIDER;
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER )) {
locationProvider = LocationManager.NETWORK_PROVIDER;
}
Log.w(TAG,"CurrentLocProvider :"+locationProvider);
location=locationManager.getLastKnownLocation(locationProvider);
Log.i(TAG,"Location :"+location);
if(location!=null){
Log.i(TAG,"Location Lat :"+location.getLatitude());
latitude = Double.toString(location.getLatitude());
longitude = Double.toString(location.getLongitude());
txtView.append("\nLatitude : "+latitude+"\n Longitude : "+longitude);
}
After some time
public void onLocationChanged(Location location) {
Log.i(TAG,"Lat :"+location.getLatitude();
Log.i(TAG,"Long :"+location.getLongitude();
}
Also giving me wrong location.Some times USA,In another device china.In Different device it is giving different results?
How can i get the currect result?
Thanks,
aare.
Hm, it is very unlikely (but possible) to give you wrong coordinates - if the device GPS is not working properly.
BTW - the method getLastKnownLocation may return completely wrong data, as described in another stackoverflow question:
this location can be "out-of-date", since the method returns the location when the GPS was used the last time.
Give a look at onStatusChanged, also. You may output the status changes of the GPS to get a better vision of what's going on.
You should also check if network location provider is enabled (users can have this disabled in settings):
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
Or even better: use LocationManager.PASSIVE_PROVIDER.