这个问题的常见答案是您必须重新启动设备.
当然你不能告诉你的用户重启设备以使你的应用程序运行,所以我的解决方案是使用HTTP回退,这是我在我的代码中使用的AsyncTask.
你必须根据你的情况修改它,因为我从位置查找地址而不是相反.
private class GetAddressPositionTask extends
AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected LatLng doInBackground(String... plookupString) {
String lookupString = plookupString[0];
final String lookupStringUriencoded = Uri.encode(lookupString);
LatLng position = null;
// best effort zoom
try {
if (geocoder != null) {
List
addresses = geocoder.getFromLocationName(lookupString, 1);
if (addresses != null && !addresses.isEmpty()) {
Address first_address = addresses.get(0);
position = new LatLng(first_address.getLatitude(),
first_address.getLongitude());
}
} else {
Log.e(TAG, "geocoder was null, is the module loaded? "
+ isLoaded);
}
} catch (IOException e) {
Log.e(TAG, "geocoder failed, moving on to HTTP");
}
// try HTTP lookup to the maps API
if (position == null) {
HttpGet httpGet = new HttpGet(
"http://maps.google.com/maps/api/geocode/json?address="
+ lookupStringUriencoded + "&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
// Log.d("MAPSAPI", stringBuilder.toString());
jsonObject = new JSONObject(stringBuilder.toString());
if (jsonObject.getString("status").equals("OK")) {
jsonObject = jsonObject.getJSONArray("results")
.getJSONObject(0);
jsonObject = jsonObject.getJSONObject("geometry");
jsonObject = jsonObject.getJSONObject("location");
String lat = jsonObject.getString("lat");
String lng = jsonObject.getString("lng");
// Log.d("MAPSAPI", "latlng " + lat + ", "
// + lng);
position = new LatLng(Double.valueOf(lat),
Double.valueOf(lng));
}
} catch (JSONException e) {
Log.e(TAG, e.getMessage(), e);
}
}
return position;
}
@Override
protected void onPostExecute(LatLng result) {
Log.i("GEOCODE", result.toString());
super.onPostExecute(result);
}
};