I built the Application Which take lat lng from gps but most of time it does not work is there Any way to get lat lng from internet..
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListenerGps);
There is Mainly two ways to get current location.
1> Using GPS
see this link for get location using GPS
In GPS it Sometime not give exactlocation so if your network enable then it try to get location from network provider. for eg. when you are in basement that time GPS sometime not give exact location.
2>Using Fused Location
see this link for get Location using Fused location
Fused Location is mostly use when you want to continue get location in some time interval.
Related
I am conducting test where we are compering GPS position of Android phone and GPS device which we would like to integrate in our hardware. But for the test to be accurate, phone need to use only GPS and not cell towers and WiFi.
Here is the code, where I set which service does the phone use.
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_MILLISECONDS_BEFORE_UPDATE_LOCATION,
MIN_METERS_BEFORE_UPDATE_LOCATION,
new MyLocationListener(this));
So will phone use only GPS to get it's location? I can' turn WIFI off, because phones are sending current locations to our data base. Both GPS device and phone are on a fixed location at the time of test.
I know that there are already questions how to use GPS for acquiring location, but I would like to make sure, that phone is using just GPS.
It can find location by only using GPS (Assuming that location is reachable, for example not underground or something like that) By using internet it just finds location faster. Since your device is stationary you should be ok.
Read more about it here.
default it use gps and if gps not working it will use internet so you dont need wright something
Use the LocationManager.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
You may also want to add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method.
My situation is like that the user has their tablet with them all day and its gps or location can be turned off whole time to save battery, and then wanna post something using my application which requires them to turn on the location and then upon posting I wanna get the latitude and longitude of the device. Using LocationManager when I get the location its null. Any help would be appreciated.
I'm using the below code to get the location:
LocationManager lm = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
As I've checked around, it seems I have to put a listenere to update the device location so when I get the last known location it wouldn't be null, but how can I do that if the device's location is off before using the application? I wanna get the location where the user is posting using the application.
EDIT: I've changed my code to below, but loc is always null and loc2 always returns location. why is that?
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location loc2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
and this is the listener:
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
You might want to consider using Network Location as well as GPS Location, as many users just keep the GPS radio off to conserve battery.
With Location Services set in "Battery Saving" mode, the device can get accurate geo-location data using Wi-Fi if available.
If you use Network Location data as well as GPS radio data, you would have a much better chance of getting location data when you need it.
However, if Location Services were disabled completely prior to being turned on in order to post something using your app, then you would need to register a Location Listener to get the first instance of location data.
Take a look at this post to see how to use both Network and GPS Location:
Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location
GPS rarely works indoors. If your users are "visiting shops" then it makes sense the GPS provider will never return a valid location as there is no GPS signal available indoors.
The network provider uses both Wifi and cell towers, so as long as they at least have cell service, you should return a location. That explains why you are always able to retrieve a location update with the network provider.
I would recommend you look into the Fused Provider which is the newest Location API released by Google. This API will automatically handle switching between GPS, network, and passive location listeners for you based on both what settings the user has enabled and which provider gives the best data given the users situation.
I get location in LocationListener configured with following code:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 5.0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 5.0, locationListener);
Locations which I get are innacurate very often. Accuracy in location for "gps" provider is about 10-20 m, but in real it can be about 500 m. And in native Android application Google Maps I see my location accurate enough.
I have suspicion that Google use some another API. Is there present another way to get locations in Android?
OS: Android 4.4
Smartphone model: Doogee Voyager 2 DG310
Google apps use a proprietary provider called "Fused Location Provider" (read more here). This provider still uses the network and GPS providers present in the background.
The first location fix from network provider will most of the time be grater than 1000 m in accuracy because it comes from a BTC (cell tower). After a few seconds it should drop to around 20-50 m accuracy based on Wi-Fi. The GPS provides locations usually with max accuracy 20 m but this can increase when you're in a densely populated area with tall buildings and bad connection to GPS satellites.
I recently discovered the Smart Location Library which should provide easy access to either GPS, network or fused providers in combination with Activity Recognition to decrease battery drain.
I've taken a look at this page: http://developer.android.com/reference/android/provider/Settings.Secure.html#LOCATION_MODE , but I'm still lost.
I know that the Location mode should return an integer. Battery Saving would be 2, High Accuracy would be 3 and if GPS is off then it should return 0.
I have no problem grabbing the current location...
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String stlongitude = Double.toString(longitude);
String stlatitude = Double.toString(latitude);
But when the phone's location mode is set to Device Only then I have to use LocationManager's GPS_PROVIDER. Which is not as accurate as the NETWORK_PROVIDER. I would like to do an if, else or a case statement that allows me to get the current location mode and if it's set to High Accuracy or Battery saving, use the NETWORK_PROVIDER but if it's set to Device Only use GPS_PROVIDER.
What you are trying to do with Settings.Secure and LOCATION_MODE is the right way to get the enabled providers if you are only interested in targeting devices that are KitKat or above (API 19). But, more than likely, you actually want your app to work with devices that have earlier versions of Android.
Here's code that gets the last location using the least power-hungry provider that the user has enabled:
LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGpsEnabled && !isNetworkEnabled) {
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else {
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
NETWORK_PROVIDER is not as accurate as GPS,
The first thing to note is that NETWORK_PROVIDER is not as accurate as GPS, according to the developer docs, network prodiver uses
NETWORK_PROVIDER
This provider determines location based on availability of cell tower
and WiFi access points. Results are retrieved by means of a network
lookup.
The most accurate provider you can use when retrieving the users, location is the GPS, which is defined by the Andorid Developer docs as:
GPS_PROVIDER
This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission ACCESS_FINE_LOCATION.
Manipulating the provider
With that in mind, the following hints will enable you to manoeuvre the logic any way you wish:
1) To set the location manager to recieve updates using best provider the device is capable of using, you can call
/* Getting the name of the BEST provider available */
provider = locationManager.getBestProvider(criteria, true);
/* Getting Current Location using the best provider available */
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 300000, 0, this);
2) If you which to force network provider, set your location manager to ask for network provider update like this:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this);
3) Similarly, if you which to force GPS use:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
I think if you are looking for LOCATION_MODE, not a provider, you should check out this question and response: Change Location Mode to High Accuracy Programmatically Android
Because even if all your providers are enabled, the location mode could mean that it would fail to get a good location if you are in a building and the mode is LOCATION_MODE_SENSORS_ONLY.
I am implementing gps provider getting latitude and longitude from device i am already get the information in device i can implemented network provider that is the problem how can implemented using network provider
The code is basically the same. Obtain a provider (gps, network, etc.), call getLastKnownLocation. Additionally you can request location updates for this provider. But you can't test this using the emulator, you'll need a phone.
LocationManager manager = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE);
manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// additionally (you have to implement LocationListener)
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0.0f, new LocationListener() ...)