”The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform.“
這裡說到了要有個backend服務,但並有說此服務要怎麼得到,罈子上有人覺得要用google map api.
在下面兩個連結中,也有外國朋友提到用map api!
The documentation states : The Geocoder class requires a backend service that is not included in the core android framework, how/where can I obtain such a service?
It seems It's a bug in the emulator for 2.2 and there is a bug fix about it, for details
seehttp://code.google.com/p/android/issues/detail?id=8816
I have found a workaround to the issue. I used the standard google api:
http://code.google.com/apis/maps/documentation/geocoding/
I have created a method that received a String address like "220+victoria+square" and returns a JSONObject with the response of the HTTP Call
public static JSONObject getLocationInfo(String address) { </p><p> HttpGet httpGet = new HttpGet("http://maps.google.<br /> + "com/maps/api/geocode/json?address=" + address<br /> + "ka&sensor=false");<br /> HttpClient client = new DefaultHttpClient();<br /> HttpResponse response;<br /> StringBuilder stringBuilder = new StringBuilder(); </p><p> try {<br /> response = client.execute(httpGet);<br /> HttpEntity entity = response.getEntity();<br /> InputStream stream = entity.getContent();<br /> int b;<br /> while ((b = stream.read()) != -1) {<br /> stringBuilder.append((char) b);<br /> }<br /> } catch (ClientProtocolException e) {<br /> } catch (IOException e) {<br /> } </p><p> JSONObject jsonObject = new JSONObject();<br /> try {<br /> jsonObject = new JSONObject(stringBuilder.toString());<br /> } catch (JSONException e) {<br /> // TODO Auto-generated catch block<br /> e.printStackTrace();<br /> } </p><p> return jsonObject;<br /> }
After executing this, another method converts that JSONObject into a GeoPoint.
public static GeoPoint getGeoPoint(JSONObject jsonObject) { </p><p> Double lon = new Double(0);<br /> Double lat = new Double(0); </p><p> try { </p><p> lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)<br /> .getJSONObject("geometry").getJSONObject("location")<br /> .getDouble("lng"); </p><p> lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)<br /> .getJSONObject("geometry").getJSONObject("location")<br /> .getDouble("lat"); </p><p> } catch (JSONException e) {<br /> // TODO Auto-generated catch block<br /> e.printStackTrace();<br /> } </p><p> return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)); </p><p> }
However this solution is extremely nasty and coupled..
PS. you should add
<uses-library android:name="com.google.android.maps"></uses-library>
to AndroidManifest.xml or application could not find GeoPoint class.
順便推薦篇android地圖開發入門文章:
http://mobiforge.com/developing/story/using-google-maps-android