2012-06-01 40 views
1

以下是我的代碼。對於某些地址,以下代碼在Android設備API級別-8中工作。但對於某些地址,地址值返回爲[] .BUt從iPhone調用同一服務器時,我沒有遇到這種問題。使用地理編碼器的Android獲取位置指針

Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 
List<Address> address = geocoder.getFromLocationName(MAPADDRESS, 1); 

如何在Android設備上解決此問題。

回答

0

這裏的問題是您在查詢請求只有一個地址

List<Address> address = geocoder.getFromLocationName(MAPADDRESS, 1); 

Android將嘗試找到在這個case.You只有一個地址應在第二次也許該提高2或3

或第三個位置,你將獲得成功。

將其更改爲

List<Address> address = geocoder.getFromLocationName(MAPADDRESS, 3); 
+0

謝謝VIPUL烏拉圭回合answer.I無線本地環路與此嘗試。 – Manjeet

+0

我試圖與以下: 列表

地址= geocoder.getFromLocationName(MAPADDRESS,3)和 列表
地址= geocoder.getFromLocationName(MAPADDRESS,15) ,但仍是露出adress.size()= 0 – Manjeet

+0

你可以在這裏粘貼你的代碼嗎?這將有助於解決這個問題。 –

0

只需使用下面的方法......同時呼籲給字符串輸入

public static JSONObject getLocationInfo(String address) { 
      StringBuilder stringBuilder = new StringBuilder(); 
      try { 

      address = address.replaceAll(" ","%20");  

      HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); 
      HttpClient client = new DefaultHttpClient(); 
      HttpResponse response; 
      stringBuilder = new StringBuilder(); 


       response = client.execute(httppost); 
       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; 
     } 
+0

非常感謝您的回覆。謝謝Rajesh.It的工作。 – Manjeet

+0

很高興聽到。它是幫助你很多知道..爲什麼你不選擇這個作爲答案。 – itsrajesh4uguys

+0

其實我是新來的,所以不知道。從下次我會肯定的。謝謝。 – Manjeet

相關問題