Find Addresses Of Given Latitude Too Longitude Using Android.Location.Geocoder

Posted on

This illustration permit users to enter latitude together with longitude, together with thence discovery the addresses that are known to depict the expanse directly surrounding the given latitude together with longitude, using getFromLocation (double latitude, double longitude, int maxResults) method of android.location.Geocoder.


android.location.Geocoder is a bird for treatment geocoding together with contrary geocoding. Geocoding is the procedure of transforming a street address or other description of a place into a (latitude, longitude) coordinate. Reverse geocoding is the procedure of transforming a (latitude, longitude) coordinate into a (partial) address. The total of item inwards a contrary geocoded place description may vary, for illustration i powerfulness comprise the total street address of the closest building, spell unopen to other powerfulness comprise entirely a urban center mention together with postal code. The Geocoder bird requires a backend service that is non included inwards the heart android framework. The Geocoder inquiry methods volition provide an empty listing if in that place no backend service inwards the platform. Use the isPresent() method to decide whether a Geocoder implementation exists.



com.blogspot.android_er.androidlatlongtoaddress.MainActivity.java

package com.blogspot.android_er.androidlatlongtoaddress;  import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast;  import java.io.IOException; import java.util.ArrayList; import java.util.List;  world bird MainActivity extends AppCompatActivity {      Geocoder geocoder;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          terminal EditText latText = (EditText)findViewById(R.id.latText);         terminal EditText lonText = (EditText)findViewById(R.id.lonText);         Button btnFind = (Button)findViewById(R.id.find);          terminal ListView listResult = (ListView)findViewById(R.id.listResult);          geocoder = novel Geocoder(this);           btnFind.setOnClickListener(new View.OnClickListener(){             @Override             world void onClick(View v) {                 String strLat = latText.getText().toString();                 String strLon = lonText.getText().toString();                  boolean parsable = true;                 Double lat = null, lon = null;                  try{                     lat = Double.parseDouble(strLat);                 }catch (NumberFormatException ex){                     parsable = false;                     Toast.makeText(MainActivity.this,                             "Latitude does non comprise a parsable double",                             Toast.LENGTH_LONG).show();                 }                   try{                     lon = Double.parseDouble(strLon);                 }catch (NumberFormatException ex){                     parsable = false;                     Toast.makeText(MainActivity.this,                             "Longitude does non comprise a parsable double",                             Toast.LENGTH_LONG).show();                 }                  if(parsable){                     Toast.makeText(MainActivity.this,                             "find " + lat + " : " + lon,                             Toast.LENGTH_LONG).show();                      List<Address> geoResult = findGeocoder(lat, lon);                     if(geoResult != null){                         List<String> geoStringResult = novel ArrayList<String>();                         for(int i=0; i < geoResult.size(); i++){                             Address thisAddress = geoResult.get(i);                             String stringThisAddress = "";                             for(int a=0; a < thisAddress.getMaxAddressLineIndex(); a++) {                                 stringThisAddress += thisAddress.getAddressLine(a) + "\n";                             }                              stringThisAddress +=                                     "CountryName: " + thisAddress.getCountryName() + "\n"                                     + "CountryCode: " + thisAddress.getCountryCode() + "\n"                                     + "AdminArea: " + thisAddress.getAdminArea() + "\n"                                     + "FeatureName: " + thisAddress.getFeatureName();                             geoStringResult.add(stringThisAddress);                         }                          ArrayAdapter<String> adapter = novel ArrayAdapter<String>(MainActivity.this,                                 android.R.layout.simple_list_item_1, android.R.id.text1, geoStringResult);                          listResult.setAdapter(adapter);                     }                  }              }         });     }      someone List<Address> findGeocoder(Double lat, Double lon){         terminal int maxResults = 5;         List<Address> addresses = null;         drive {             addresses = geocoder.getFromLocation(lat, lon, maxResults);         } grab (IOException e) {             e.printStackTrace();         } grab (IllegalArgumentException e) {             e.printStackTrace();         }          provide addresses;     }  }  

layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:padding="10dp"     tools:context=".MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <EditText         android:id="@+id/latText"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:inputType="numberSigned|numberDecimal"         android:hint="Latitude" />      <EditText         android:id="@+id/lonText"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:inputType="numberSigned|numberDecimal"         android:hint="Longitude" />      <Button         android:id="@+id/find"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="find Address"/>      <ListView         android:id="@+id/listResult"         android:layout_width="match_parent"         android:layout_height="match_parent"/>  </LinearLayout>  

 together with thence discovery the addresses that are known to depict the expanse directly surrounding the g Find addresses of given latitude together with longitude using android.location.GeocoderDownload the files (Android Studio Format) .


Related:
Get ISO province code for the given latitude/longitude, using GeoNames Web Service