android avd2.2擷取街道資訊:Geocoder, “the service is not available” && Geocoder返回List為null,得不到街道資訊

來源:互聯網
上載者:User

”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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.