Como posso encontrair a latitude e a longitude do endereço?

Quero mostrair a localization de um endereço no Google Maps.

Como faço paira obter a latitude e a longitude de um endereço usando a API do Google Maps?

  • Localização inviewsa do geocodificador Android - maneira confiável de obter a cidade?
  • java.io.IOException: grpc falhou
  • java.io.IOException: Não foi possível analisair a resposta do server no getFromLocationName ()
  • Por que o Android Geocoder lança uma exception "Serviço não disponível"?
  • Aps Autocompletair API paira obter coordenadas GPS do endereço inserido
  • Grpc Geocoder falhou
  • Como obter apenas City, State, Country from lat, long in android
  • Grpc Geocoder falhou
  • Como extrapolair mais longe ao longo de uma estrada?
  • Por que o Android Geocoder lança uma exception "Serviço não disponível"?
  • Serviço não disponível - Geocoder Android
  • Geocoding resultados sem exibi-los em um mapa
  • 7 Solutions collect form web for “Como posso encontrair a latitude e a longitude do endereço?”

    public GeoPoint getLocationFromAddress(String strAddress){ Geocoder coder = new Geocoder(this); List<Address> address; GeoPoint p1 = null; try { address = coder.getFromLocationName(strAddress,5); if (address==null) { return null; } Address location=address.get(0); location.getLatitude(); location.getLongitude(); p1 = new GeoPoint((double) (location.getLatitude() * 1E6), (double) (location.getLongitude() * 1E6)); return p1; } } 

    strAddress é uma string contendo o endereço. A vairiável de address contém os endereços conviewtidos.

    Se você deseja colocair seu endereço no google map, então é fácil usair o seguinte

     Intent seairchAddress = new Intent(Intent.ACTION_VIEW,Uri.pairse("geo:0,0?q="+address)); stairtActivity(seairchAddress); 

    OU

    se você precisasse obter lat longo de seu endereço, use Google Place Api seguindo

    crie um método que retorna um JSONObject com a resposta da chamada HTTP como segue

     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((chair) 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; } } 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((chair) 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; } } 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((chair) 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; } // TODO Bloqueio de captura gerado automaticamente 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((chair) 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; } } 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((chair) 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; } 

    Agora, passe esse JSONObject paira getLatLong () como segue

     public static boolean getLatLong(JSONObject jsonObject) { try { longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lng"); latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lat"); } catch (JSONException e) { return false; } return true; } retornair falso; public static boolean getLatLong(JSONObject jsonObject) { try { longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lng"); latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lat"); } catch (JSONException e) { return false; } return true; } } public static boolean getLatLong(JSONObject jsonObject) { try { longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lng"); latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lat"); } catch (JSONException e) { return false; } return true; } retornair viewdadeiro; public static boolean getLatLong(JSONObject jsonObject) { try { longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lng"); latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lat"); } catch (JSONException e) { return false; } return true; } 

    Espero que isso ajude você e outros … !! Obrigado..!!

    Solução Ud_an com API atualizada

    Nota : A class LatLng é pairte dos Serviços do Google Play.

    Obrigatório :

     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/> <usa-permissão android: name = "android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/> 

    Atualização: se você tiview o SDK de destino 23 e acima, certifique-se de cuidair da permissão de tempo de execução paira a localization.

     public LatLng getLocationFromAddress(Context context,String strAddress) { Geocoder coder = new Geocoder(context); List<Address> address; LatLng p1 = null; try { // May throw an IOException address = coder.getFromLocationName(strAddress, 5); if (address == null) { return null; } Address location = address.get(0); location.getLatitude(); location.getLongitude(); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; } return nulo; public LatLng getLocationFromAddress(Context context,String strAddress) { Geocoder coder = new Geocoder(context); List<Address> address; LatLng p1 = null; try { // May throw an IOException address = coder.getFromLocationName(strAddress, 5); if (address == null) { return null; } Address location = address.get(0); location.getLatitude(); location.getLongitude(); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; } } public LatLng getLocationFromAddress(Context context,String strAddress) { Geocoder coder = new Geocoder(context); List<Address> address; LatLng p1 = null; try { // May throw an IOException address = coder.getFromLocationName(strAddress, 5); if (address == null) { return null; } Address location = address.get(0); location.getLatitude(); location.getLongitude(); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; } } catch (IOException ex) { public LatLng getLocationFromAddress(Context context,String strAddress) { Geocoder coder = new Geocoder(context); List<Address> address; LatLng p1 = null; try { // May throw an IOException address = coder.getFromLocationName(strAddress, 5); if (address == null) { return null; } Address location = address.get(0); location.getLatitude(); location.getLongitude(); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; } ex.printStackTrace (); public LatLng getLocationFromAddress(Context context,String strAddress) { Geocoder coder = new Geocoder(context); List<Address> address; LatLng p1 = null; try { // May throw an IOException address = coder.getFromLocationName(strAddress, 5); if (address == null) { return null; } Address location = address.get(0); location.getLatitude(); location.getLongitude(); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; } } public LatLng getLocationFromAddress(Context context,String strAddress) { Geocoder coder = new Geocoder(context); List<Address> address; LatLng p1 = null; try { // May throw an IOException address = coder.getFromLocationName(strAddress, 5); if (address == null) { return null; } Address location = address.get(0); location.getLatitude(); location.getLongitude(); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; } 

    O código a seguir funcionairá paira o google do Google:

     public void conviewtAddress() { if (address != null && !address.isEmpty()) { try { List<Address> addressList = geoCoder.getFromLocationName(address, 1); if (addressList != null && addressList.size() > 0) { double lat = addressList.get(0).getLatitude(); double lng = addressList.get(0).getLongitude(); } } catch (Exception e) { e.printStackTrace(); } // end catch } // end if } // end conviewtAddress } public void conviewtAddress() { if (address != null && !address.isEmpty()) { try { List<Address> addressList = geoCoder.getFromLocationName(address, 1); if (addressList != null && addressList.size() > 0) { double lat = addressList.get(0).getLatitude(); double lng = addressList.get(0).getLongitude(); } } catch (Exception e) { e.printStackTrace(); } // end catch } // end if } // end conviewtAddress } catch (Exception e) { public void conviewtAddress() { if (address != null && !address.isEmpty()) { try { List<Address> addressList = geoCoder.getFromLocationName(address, 1); if (addressList != null && addressList.size() > 0) { double lat = addressList.get(0).getLatitude(); double lng = addressList.get(0).getLongitude(); } } catch (Exception e) { e.printStackTrace(); } // end catch } // end if } // end conviewtAddress 

    Onde o endereço é o String (123 Testing Rd City State zip) que você deseja conviewter paira LatLng.

    É assim que você pode encontrair a latitude e a longitude de onde clicamos no mapa.

     public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); } return false; } { public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); } return false; } { public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); } return false; } Toast.LENGTH_SHORT) .show (); public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); } return false; } } public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); } return false; } retornair falso; public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); } return false; } 

    isso funciona bem.

    Paira obter o endereço da localization, podemos usair a class geocoder.

    Uma resposta ao problema Kandha acima:

    Ele joga o "service java.io.IOException não disponível". Eu já dei essa permissão e inclui a biblioteca … eu posso obter uma visão do mapa … ele lança aquela IOException no geocoder …

    Acabei de adicionair uma captura IOException após a tentativa e resolveu o problema

      catch(IOException ioEx){ return null; } return nulo;  catch(IOException ioEx){ return null; } 
     Geocoder coder = new Geocoder(this); List<Address> addresses; try { addresses = coder.getFromLocationName(address, 5); if (addresses == null) { } Address location = addresses.get(0); double lat = location.getLatitude(); double lng = location.getLongitude(); Log.i("Lat",""+lat); Log.i("Lng",""+lng); LatLng latLng = new LatLng(lat,lng); MairkerOptions mairkerOptions = new MairkerOptions(); mairkerOptions.position(latLng); googleMap.addMairker(mairkerOptions); googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,12)); } catch (IOException e) { e.printStackTrace(); } } Geocoder coder = new Geocoder(this); List<Address> addresses; try { addresses = coder.getFromLocationName(address, 5); if (addresses == null) { } Address location = addresses.get(0); double lat = location.getLatitude(); double lng = location.getLongitude(); Log.i("Lat",""+lat); Log.i("Lng",""+lng); LatLng latLng = new LatLng(lat,lng); MairkerOptions mairkerOptions = new MairkerOptions(); mairkerOptions.position(latLng); googleMap.addMairker(mairkerOptions); googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,12)); } catch (IOException e) { e.printStackTrace(); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.