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.
Related
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
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.
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 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
I am trying to find distance between two locations in android using the LocationListener and related classes and methods.
I was able to get the coordinates of the given address by using geo coding class. And also was able to get distance between two addresses using the distanceto method.
2.Now I want to give address and get coordinates and then get the distance between previous given address location and the current location (which changes) in real time. since there is no current location method in android i got to use Location Listener.
the want the distance between the given address location coordinate and the real time changing location when moving using locationlistener and other location classes and methods. I wrote and deployed on a device with all permissions in manifest, but the gps gets disabled as soon as i start the app. Below is my code
//below code to get the address location coordinates which is succesful
int i=1;
Geocoder geo=new Geocoder(this);
List<Address> addressList = geo.getFromLocationName(adrs,i);
Address address = addressList.get(0);
if(address.hasLatitude() && address.hasLongitude()){
lat = address.getLatitude();
lon = address.getLongitude();
String adlalo= "Latitude of above address: "+lat +" " +
" "+"Longitude of above address: "+lon;
t2.setText(adlalo);
}
// and below is the location listener class
public class LoctListner implements LocationListener
{
public void onLocationChanged(Location loc)
{
t3 = (TextView) findViewById(R.id.textView3);
l1.setLatitude(lat);
l1.setLongitude(lon);
float d=l1.distanceTo(loc);
String s="the distance between starting and ending point is "+d;
t3.setText(s);
}
}
help me where i made the mistake. Unable to see the log since i got to take the device out to test.since this app needs a moving location.Thanks in advance
Unable to see the log since i got to take the device out to test.since this app needs a moving location
Perhaps you forgot to call requestLocationUpdates() ?
Unable to see the log
You have not put any Logs . Use Logs.D("YOURTAG", "Logs statements");
Location l2 = new Location("");
l2.setLatitude(loc.getLatitude());
l2.setLongitude(loc.getLongitude());
^ This is not required
float door = l1.distanceTo(loc);
You have to wait for a few minutes till the GPS Icon stops animating. This means that a fix has been attained. So check if loc is null
The Geocoder is useless.You can not get address by latlng with the Geocoder.
You can use Gson to parse a json doc from Google maps Api.The json contains your address info.