11

我一直試圖對一個字符串進行地理編碼來獲取它的座標,但是我的程序總是崩潰,因爲當我嘗試使用getFromLocationName()時它返回null。我一直試圖解決這個問題,但沒有任何工作。這是我的代碼Android Geocoder getFromLocationName總是返回null

public class MainActivity extends Activity { 

    private GoogleMap mMap; 

    List<Address> addresses; 
    MarkerOptions miami; 
    String myLocation = "Miami,Florida"; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if (mMap == null) { 
      mMap = ((MapFragment) getFragmentManager().findFragmentById(
        R.id.map)).getMap(); 
     } 

     if (mMap != null) { 

      Geocoder geocoder = new Geocoder(this); 


      double latitude = 0; 
      double longitude = 0; 

      while(addresses==null){ 
      try { 
       addresses = geocoder.getFromLocationName(myLocation, 1); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      } 
      Address address = addresses.get(0); 
      if (addresses.size() > 0) { 
       latitude = address.getLatitude(); 
       longitude = address.getLongitude(); 
      } 
      LatLng City = new LatLng(latitude, longitude); 

      miami = new MarkerOptions().position(City).title("Miami"); 

      mMap.addMarker(miami); 

      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15)); 

     } 
} 
+0

人要回答這個問題嗎? – anishk25 2013-03-03 15:42:30

回答

22

Geocoder並不總是返回一個值。您可以嘗試在for循環中發送3次請求。我應該能夠返回至少一次。如果不是那麼,他們可能是一個連接問題,也可能是其他問題,如服務器無法回覆您的請求。試一試,看看這些線程:

Geocoder doesn't always return a valuegeocoder.getFromLocationName returns only null

更新時間:

我有一個while循環很好,但我曾經嘗試它最大爲10倍。有時,即使它連接到互聯網,它也不會返回任何東西。然後,我用this更可靠的方式來獲得地址每次:

public JSONObject getLocationInfo() { 

     HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true"); 
     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) { 
      e.printStackTrace(); 
     } 
     return jsonObject; 
    } 

我把它叫做如下:

JSONObject ret = getLocationInfo(); 
JSONObject location; 
String location_string; 
try { 
    location = ret.getJSONArray("results").getJSONObject(0); 
    location_string = location.getString("formatted_address"); 
    Log.d("test", "formattted address:" + location_string); 
} catch (JSONException e1) { 
    e1.printStackTrace(); 

} 

希望這有助於。我也厭倦了依靠地理編碼器。這對我有效。 如果您用緯度和經度座標替換URL並在Web瀏覽器中查看返回的JSON對象。你會看到剛剛發生的事情。

+0

但我有一個while循環來確保我爲geocoder獲得一個值,並且它會一直循環。 – anishk25 2013-03-06 04:18:16

+0

查看我的更新答案。希望它能解決你想要達到的目標。 – 2013-03-06 06:04:33

+1

我面臨着同樣的[問題](http://stackoverflow.com/questions/15110528/service-not-available-while-calling-geocoder-getfromlocation)。我發現設備上的NetworkLocator因某種原因停止工作或死亡(不在我們手中)。您可以通過重新啓動設備來確認這一點。它現在必須工作(因爲'NetworkLocator也重新啓動')。 – 2013-03-09 05:28:10

相關問題