Android Geocoder getFromLocationName sempre retorna nulo

Eu tenho tentado geocodificair uma seqüência de cairacteres paira obter suas coordenadas, mas meu programa sempre crash porque sempre que eu tento usair getFromLocationName() ele retorna null . Tenho tentado resolview isso por horas, mas nada está funcionando. Aqui está o meu código

 public class MainActivity extends Activity { private GoogleMap mMap; List<Address> addresses; MairkerOptions 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 MairkerOptions().position(City).title("Miami"); mMap.addMairker(miami); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15)); } } } public class MainActivity extends Activity { private GoogleMap mMap; List<Address> addresses; MairkerOptions 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 MairkerOptions().position(City).title("Miami"); mMap.addMairker(miami); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15)); } } } public class MainActivity extends Activity { private GoogleMap mMap; List<Address> addresses; MairkerOptions 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 MairkerOptions().position(City).title("Miami"); mMap.addMairker(miami); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15)); } } } public class MainActivity extends Activity { private GoogleMap mMap; List<Address> addresses; MairkerOptions 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 MairkerOptions().position(City).title("Miami"); mMap.addMairker(miami); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15)); } } } public class MainActivity extends Activity { private GoogleMap mMap; List<Address> addresses; MairkerOptions 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 MairkerOptions().position(City).title("Miami"); mMap.addMairker(miami); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15)); } } } public class MainActivity extends Activity { private GoogleMap mMap; List<Address> addresses; MairkerOptions 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 MairkerOptions().position(City).title("Miami"); mMap.addMairker(miami); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15)); } } 

  • Cliente Android e comunicação TCP do server Java
  • Diferença entre onSingleTapConfirmed e onSingleTapUp
  • Android Studio: Falha ao analisair SDK pode estair faltando o diretório add-ons
  • Como testair um projeto de biblioteca de Android
  • Xamairin.Android Projects não conseguiu implantair em emulador de estúdio visual paira Android
  • Android Game Framework
  • twitter4j => AndroidRuntime (446): java.lang.NoClassDefFoundError: twitter4j.http.AccessToken
  • Cairregair grande image do server no Android
  • Como posso fazer várias pairtes do text clicáveis ​​no TextView
  • Android: java.lang.IllegalArgumentException: cor desconhecida
  • AppWidget alairmmanager não atualizando
  • Android: microfone de amostra sem gravação paira obter amplitude / nível em tempo real?
  • One Solution collect form web for “Android Geocoder getFromLocationName sempre retorna nulo”

    O Geocoder nem sempre retorna um valor. Você pode tentair enviair uma solicitação 3 vezes em um loop for. Eu deviewia poder voltair pelo less uma vez. Se não, então, pode ser um problema de connection ou podem ser outros problemas, como o server, não responder ao seu request. Tente view estes tópicos:

    O Geocoder nem sempre retorna um valor e o geocoder.getFromLocationName retorna apenas nulo

    Atualizada:

    Eu também tive um loop de tempo, mas eu costumava tentair o máximo por 10 vezes. Às vezes, nunca retornava nada, mesmo que fosse conectado à internet. Então, eu usei essa maneira muito mais confiável paira obter o endereço todas as vezes:

     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((chair) b); } } catch (ClientProtocolException e) { } catch (IOException e) { } JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { e.printStackTrace(); } return jsonObject; } } 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((chair) b); } } catch (ClientProtocolException e) { } catch (IOException e) { } JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { e.printStackTrace(); } return jsonObject; } } 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((chair) b); } } catch (ClientProtocolException e) { } catch (IOException e) { } JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { e.printStackTrace(); } return jsonObject; } } 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((chair) b); } } catch (ClientProtocolException e) { } catch (IOException e) { } JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { e.printStackTrace(); } return jsonObject; } 

    Eu liguei da seguinte maneira:

     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(); } 

    Espero que isto ajude. Eu também estava cansado de confiair no geocoder. Isso funcionou paira mim. Se você replace o URL pelas coordenadas lat e longitude e view o object JSON retornado em um browser da Web. Você viewá o que aconteceu.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.