My app has been ported from Android to blackberry and it works fine...
the only thing not working is the GPS location which works on the android....
the app displays a message to the using that we are trying to find the location and if no location is found we tell the user that we could not retrieve a location..
here is the code
private void gpsLocation()
{
//Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Provide Criteria
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if(location != null){
onLocationChanged(location);
} else{
Log.i(TAG, "no GPS location available - waiting for GPS fix");
Toast.makeText(getApplicationContext(), R.string.gpsWaitingToast, Toast.LENGTH_SHORT).show();
if (Debug.GPS_USE_MONTREAL) {
currentLatitude = Double.valueOf(Debug.GPS_LAT_MONTREAL); currentLongitude = Double.valueOf(Debug.GPS_LNG_MONTREAL); // montreal
}
waitForGpsFix(GPSFIX_DELAY);
}
its not working,, I restarted the app ,, did it next to a window and all that stuff..
nothing works..
your advise and help is greatly appreciated.
Regards
There was previously a bug where LocationManager.NETWORK_PROVIDER returned null, which may still be returned by the locationManager.getBestProvider() method. This should be fixed in the newer OS builds, however.
If you'd like to fix your code immediately, all you would need to do would be to set:
provider = LocationManager.GPS_PROVIDER
as in the early stages, GPS_PROVIDER was the only provider that was implemented in the player.
Related
I am trying to retrieve Location as follows:
LocationManager locationManager = (LocationManager)MyApp.getAppContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
String provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// NOTE: the location below is null !!
Location location = locationManager.getLastKnownLocation(provider);
String lat = location.getLatitude();
String lng = location.getLongitude();
}
The 'location' above is null. Why?
But when i open the (Google) Maps app - it shows location correctly, and even a notification icon (looks like a exclamation mark) shows up.
And there is a GPS Coordinates app - which also shows up blank.
I have turned on Settings > 'Location' on the phone. If it matters this is Android 4.4.4 Sony Experia phone.
Well, it seems that you're not checking properly if the GPS is working or not. The way you should do it:
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if (!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
//GPS is not enabled !!
}
You can also create an AlertDialog so the user is aware that the GPS is not enabled and you can send him to the GPS settings by doing this:
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
After this, I'd implement a location listener and get coordinates, here is an example, check it out
How do I get the current GPS location programmatically in Android?
UPDATE: They might be getting coordinates through other provider (usually the one which is working better at that moment). So if the GPS it not working, just try other provider, here is an example:
Location lastKnownLocation = null;
List<String> providers = null;
if(locationManager != null) providers = locationManager.getAllProviders();
if(providers != null)
{
for(int i=0; i<providers.size(); i++)
{
if(locationManager != null) lastKnownLocation = locationManager.getLastKnownLocation(providers.get(i));
if(lastKnownLocation != null)
{
position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
break;
}
}
}
I have an application that used to locate a user's location with this code
public static Location getCurrentLocation(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
String provider = lm.getBestProvider(c, true);
Location location;
if (provider == null) {
location = new Location("gps");
location.setLatitude(U.LATITUDE);
location.setLongitude(U.LONGITUDE);
} else {
location = lm.getLastKnownLocation(provider);
if (location == null) {
location = new Location("gps");
location.setLatitude(U.LATITUDE);
location.setLongitude(U.LONGITUDE);
}
}
return location;
}
This code appears to only be returning the behavior when provider is null or location is null. My guess is that something changed in this code.
String provider = lm.getBestProvider(c, true);
I have some code very similar to yours and it stopped working on my device after the upgrade to Android 4.4 (i.e. getBestProvider returned null which it never did before).
I discovered the reason when I saw that another App displayed that the NetworkProvider wasn't available (although I had perfect connection to both wifi and 3G) and it could not get a GPS signal (obviously, because I was indoors). Then I toggled Android's location quick setting and a dialog popped up, asking me to agree to the new location management of Android 4.4 (see https://support.google.com/nexus/answer/3467281). Afterwards, my code worked as before.
I have this code for getting the long and lat coordinates.
String bestProvider;
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = lm.getBestProvider(criteria, true);
Location location = lm.getLastKnownLocation(bestProvider);
if (location == null){
}else{
geocoder = new Geocoder(this);
try {
setLat((double)location.getLatitude());
setLng((double)location.getLongitude());
}catch (Exception e) {
e.printStackTrace();
}
}
I tried using different devices. Some returns a value for lat and long. Some do not. All devices were connected to the internet and gps enabled. Do you guys have any idea on what is causing this? Thanks!
It is due to getLastKnownLocation which may or may not exists. I suggest you to request single location update. Also remember that lastknownlocation is not necessarily accurate one it may belong to 2 days before.
dev guide:
http://developer.android.com/reference/android/location/LocationManager.html
A guide about single update
http://androidexperinz.wordpress.com/2012/04/19/current-location-update/
If there is no previous captured location, then getlastknown location does not return any value.So you need to implement the location listener to capture the location.
My app only needs a coarse location service when started up.
In detail, I need the app's rough location so as to provide the users with the shop info nearby.
The location does NOT need to be updated constantly. In addition, coarse localization will be sufficient in this case.
I wish the app to choose GSM, or wifi, or GPS automatically, whichever is available.
The location service should also be one-time to save phone energy.
How may I do that?
I have tried using GPS separately.
My problem is I don't know how to stop the constantly-refreshing-location feature of GPS. I don't know how to make the phone select one out of the three methods, either.
Some sample codes or ideas are greatly appreciated.
Here's a certain point of view:
private void _getLocation() {
// Get the location manager
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
try {
lat = location.getLatitude();
lon = location.getLongitude();
} catch (NullPointerException e) {
lat = -1.0;
lon = -1.0;
}
}
This might however request a FINE_LOCATION access. So:
Another way is to use this which uses the LocationManager.
The quickest possible way is to use the Last Known location with this, I used it and it's quite fast:
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(
Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
you can use LocationClient it provides a unified/simplified (Fused) location API, check out this Video for introductory material and background or this developer guide
the main drawback is that you make you app dependent on the existence of Google Play Services on the device.
In order to get info from specific provider you should use: LocationManager.getLastKnownLocation(String provider), if you want your app to choose automatically between providers then you can add choosing of the provider with getBestProvider method. As for stopping refreshing of GPS location, I didn't quite catch. Do you need to get the location info only once or do you need to monitor location changes periodically?
EDIT: Oh by the way, if you want your location info to be up-to-date you should use requestSingleUpdate method of location manager, with specified Criteria. In this case provider should also be chosen automatically
I have a problem in getting the user's location (my location). My code is
double lat;
double lng;
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
if(!locationManager.isProviderEnabled(provider)){
locationManager.setTestProviderEnabled(provider, true);
}
boolean enabled = locationManager.isProviderEnabled(provider);
if(enabled){
Toast.makeText(LoginActivity.this,"provider enabled",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(LoginActivity.this,"provider disabled",Toast.LENGTH_LONG).show();
}
if(location!=null){
lat = location.getLatitude();
lng = location.getLongitude();
AlertDialog.Builder ab=new AlertDialog.Builder(LoginActivity.this);
ab.setMessage(Html.fromHtml("<b><font color=#ff0000>Location" +"</font></b><br>"
+location.toString()+"<br>Latitude: "+lat
+"<br>Longitude "+lng));
ab.setPositiveButton("ok",null );
Toast.makeText(LoginActivity.this,"You are at "+location.toString(),Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(LoginActivity.this,"Location not found",Toast.LENGTH_LONG).show();
}
My problem is I am getting the location as null while the application message provider is enabled. I have not found any problem in this code. I have also tested it on a device, it shows the provider is enabled and the location isn't found.
I have not implemented the location listener in the class.
Is it necessary to implement location listener in my class?
You are only getting the last known location from the phone. If this is null, which it is if no last known location is available, you are not trying to receive locations in any other way.
You should implement a LocationListener an register it to receive location updates according to this guide: http://developer.android.com/guide/topics/location/obtaining-user-location.html This will cause the phone to try to get the users location and hand it over to your application in the form of a Location object.