读书人

geoCoder.getFromLocationName return

发布时间: 2012-07-01 13:15:00 作者: rapoo

geoCoder.getFromLocationName returns null .

?

”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?

?

view plaincopy to clipboardprint?
  1. public?static?JSONObject?getLocationInfo(String?address)?{????????
  2. ????????HttpGet?httpGet?=?new?HttpGet("http://maps.google.????????????????????+?"com/maps/api/geocode/json?address="?+?address????
  3. ????????????????+?"ka&sensor=false");????????????HttpClient?client?=?new?DefaultHttpClient();????
  4. ????????HttpResponse?response;????????????StringBuilder?stringBuilder?=?new?StringBuilder();????
  5. ????????????try?{????
  6. ????????????response?=?client.execute(httpGet);????????????????HttpEntity?entity?=?response.getEntity();????
  7. ????????????InputStream?stream?=?entity.getContent();????????????????int?b;????
  8. ????????????while?((b?=?stream.read())?!=?-1)?{????????????????????stringBuilder.append((char)?b);????
  9. ????????????}????????????}?catch?(ClientProtocolException?e)?{????
  10. ????????}?catch?(IOException?e)?{????????????}????
  11. ????????????JSONObject?jsonObject?=?new?JSONObject();????
  12. ????????try?{????????????????jsonObject?=?new?JSONObject(stringBuilder.toString());????
  13. ????????}?catch?(JSONException?e)?{????????????????//?TODO?Auto-generated?catch?block????
  14. ????????????e.printStackTrace();????????????}????
  15. ????????????return?jsonObject;????
  16. ????}????
?

?

After executing this, another method converts that JSONObject into a GeoPoint.?

?

view plaincopy to clipboardprint?
  1. public?static?GeoPoint?getGeoPoint(JSONObject?jsonObject)?{????????
  2. ????????Double?lon?=?new?Double(0);????????????Double?lat?=?new?Double(0);????
  3. ????????????try?{????
  4. ????????????????lon?=?((JSONArray)jsonObject.get("results")).getJSONObject(0)????
  5. ????????????????.getJSONObject("geometry").getJSONObject("location")????????????????????.getDouble("lng");????
  6. ????????????????lat?=?((JSONArray)jsonObject.get("results")).getJSONObject(0)????
  7. ????????????????.getJSONObject("geometry").getJSONObject("location")????????????????????.getDouble("lat");????
  8. ????????????}?catch?(JSONException?e)?{????
  9. ????????????//?TODO?Auto-generated?catch?block????????????????e.printStackTrace();????
  10. ????????}????????
  11. ????????return?new?GeoPoint((int)?(lat?*?1E6),?(int)?(lon?*?1E6));????????
  12. ????}????

?

?

However this solution is extremely nasty and coupled..?
PS. you should add

view plaincopy to clipboardprint?
  1. <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

转自:http://blog.csdn.net/evanwu_85/article/details/6544325

读书人网 >移动开发

热点推荐