摘要:Android - Geocoder Service not available
今天遇到 Geocoder Service not available 問題
查詢一下,發生是需要用到的 web service 不可使用,
說明如下
The actual reason why Geocoder
was not working is because the NetworkLocator
was killed in action. Probably due to less memory or maybe you used the Task Manager to kill all services?
I'm not sure but this is a guess. I've seen this before. Last year I wrote a reconnect mechanism to load the NetworkLocator.apk and bind to the GeocoderService
. I think this change has not merged into JellyBean so this problem persists.
It can be only solved by reboot. (The NetworkLocationService
is loaded at boot)
而官網回答如下:
http://docs.huihoo.com/
the API is dependent on a web service. If such service is unavailable on the device, the API will throw a "Service not Available exception" or return an empty list of addresses. A helper method called isPresent()
was added in Android 2.3 (API level 9) to check for the existence of the service.
解法呢??請換別的方式處理吧!(目前沒研究)
或許可以透過google map api查詢看看。使用restful api 查詢或許是最快的解法(至少連網頁應該是可以吧?)
根據網路論壇討論。
https://code.google.com/p/android/issues/detail?id=38009
得到一個解如下
public static JSONObject getLocationInfo(String address) {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +address+"&ka&sensor=false");
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 {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
public static GeoPoint getGeoPoint(JSONObject jsonObject) {
Double lon = new Double(0);
Double lat = new Double(0);
try {
lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
}
GeoPoint srcGeoPoint =getGeoPoint(getLocationInfo(fromAddress.replace("\n"," ").replace(" ", "%20")));
GeoPoint destGeoPoint =getGeoPoint(getLocationInfo(CalDescription.toAddress.replace("\n"," ").replace(" ", "%20")));