我正在尝试使用GPS provider
或Network provider
获取位置,但我没有从 GPS 或网络获取位置。
这是我的代码,它在几天前工作正常,但现在它无法正常工作
我不明白我错在哪里因为所有权限都已添加到 AndroidManifest.xml 中
以下是有助于您理解的代码。
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);
}
}
这里是覆盖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<Address> 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");
}
}
即使我没有使用Geocoder
获取位置,如果我输入城市名称,则会显示“无法找到位置”。
这是诀窍,GeoCoder
正在使用模拟器,但没有在手机上工作(我尝试了2个不同的手机)。
我的项目是在API 17
中构建的,没有任何logcat错误
请给我任何提示或参考。
答案 0 :(得分:0)
也许您应该检查“位置访问”设置并允许访问您的位置?..
答案 1 :(得分:0)
虽然我无法立即告诉您的代码有什么问题,但您应该考虑使用位置库并让其他人做繁重的工作。请参阅:https://code.google.com/p/little-fluffy-location-library/