Monday, April 8, 2013

[android help] Not getting Lat and Long through either GPS or Network


I am trying to get location using GPS provider or Network provider but i didn't get location from either GPS or Network.
Here is my code which was working fine some days ago but now it's not working.
I don't understand where i am wrong because all permission are already added in AndroidManifest.xml.
Here is the code which helpful for you to understand.



public class SearchDishoom extends Header implements LocationListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchdish);




manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000000, 100, this);

locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// if gps provider is not enable then popup alertbox
buildAlertMessageNoGps();
} else {
// if gps is one then start searching
locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000000,100, locationListenerGps);
}
}

/*
* Get location (lat-long) from sharedpreference to further use
*/
prefLocation = getSharedPreferences("myLocation", MODE_WORLD_READABLE);
String userLocationLat1 = prefLocation.getString("Lat", String.valueOf(0));
String userLocationlong1 = prefLocation.getString("Long", String.valueOf(0));
String address = prefLocation.getString("Address", "Location not found not found");
userLocationLat = Double.parseDouble(userLocationLat1);
userLocationlong = Double.parseDouble(userLocationlong1);

// set lat-long value in getset class for use of another activity
gs = new GetSet();
gs.setLatitude(userLocationLat);
gs.setLangitude(userLocationlong);
if (userLocationLat == 0 && userLocationlong == 0 && address.equals("")) {
/*
* if lat-long is 0 or null then start searching using
* GPS Provider or Network Provider
*/

manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else {
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000000,100, locationListenerGps);
}
} else {
// set lat-long value in getset class for use of another activity
gs.setLatitude(userLocationLat);
gs.setLangitude(userLocationlong);
setLocationName(userLocationLat, userLocationlong);
}
}


Here is override onLocationchanged():



@Override
public void onLocationChanged(Location location) {
userLocationLat =location.getLatitude();
userLocationlong =location.getLongitude();
prefLocation = getSharedPreferences("myLocation", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = prefLocation.edit();
prefsEditor.putString("Lat", String.valueOf(userLocationLat));
prefsEditor.putString("Long", String.valueOf(userLocationlong));

gs.setLatitude(userLocationLat);
gs.setLangitude(userLocationlong);
List
addresses;
try {
addresses = new Geocoder(SearchDishoom.this, Locale.getDefault())
.getFromLocation(userLocationLat, userLocationlong, 1);
Address obj = addresses.get(0);
add = obj.getAddressLine(0);
city = obj.getLocality();
addressString = add + "," + city;
gs.setCurrentAddressString(addressString);
prefsEditor.putString("Address", addressString);
prefsEditor.commit();
tvLocation.setText(add + "," + city);
} catch (IOException e) {
showToast("Unable to find location");
}
}


Even i am not getting location using Geocoder, If i enter city name then it show me "Unable to find location".
Here is trick, GeoCoder is working on Emulator but not working on phone(i tried 2 different handset).


My project is build in API 17 and no any logcat error.
Please give me any hint or reference.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...