I want to show the location of an address in Google Maps.
How do I get the latitude and longitude of an address using the Google Maps API?
I want to show the location of an address in Google Maps.
How do I get the latitude and longitude of an address using the Google Maps API?
public void goToLocationFromAddress(String strAddress) {
//Create coder with Activity context - this
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
//Get latLng from String
address = coder.getFromLocationName(strAddress, 5);
//check for null
if (address != null) {
//Lets take first possibility from the all possibilities.
try {
Address location = address.get(0);
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
//Animate and Zoon on that map location
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
} catch (IndexOutOfBoundsException er) {
Toast.makeText(this, "Location isn't available", Toast.LENGTH_SHORT).show();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public LatLang getLatLangFromAddress(String strAddress){
Geocoder coder = new Geocoder(this, Locale.getDefault());
List<Address> address;
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return new LatLang(-10000, -10000);
}
Address location = address.get(0);
return new LatLang(location.getLatitude(), location.getLongitude());
} catch (IOException e) {
return new LatLang(-10000, -10000);
}
}
LatLang is a pojo class in this case.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> permission is not required.
I know I am too late for the contribution of 10 years old question, in the year 2021. The following code is fully complete working code.
code for activity_main.xml:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Address"
android:id="@+id/addressTV"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/addressET"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/addressTV"
android:singleLine="true"
android:hint="1600 Pennsylvania Ave NW Washington DC 20502" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Lat/Long"
android:id="@+id/addressButton"
android:layout_below="@+id/addressTV"
android:layout_toEndOf="@+id/addressTV"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/latLongTV"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/addressTV" />
AndroidManifest.xml Give following permission:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
MainActivity.java
public class MainActivity extends Activity {
Button addressButton;
TextView addressTV;
TextView latLongTV;
EditText addressET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addressET = findViewById(R.id.addressET);
addressTV = (TextView) findViewById(R.id.addressTV);
latLongTV = (TextView) findViewById(R.id.latLongTV);
addressButton = (Button) findViewById(R.id.addressButton);
addressButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String address = addressET.getText().toString();
GeocodingLocation locationAddress = new GeocodingLocation();
locationAddress.getAddressFromLocation(address,
getApplicationContext(), new GeocoderHandler());
}
});
}
private class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
String address;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
address = bundle.getString("address");
break;
default:
address = null;
}
latLongTV.setText(address);
}
}
}
GeocodingLocation.java
public class GeocodingLocation {
public static void getAddressFromLocation(String locationAddress, Context context, Handler handler) {
Thread thread = new Thread() {
@Override
public void run() {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List addressList = geocoder.getFromLocationName(locationAddress,1);
if (addressList != null && addressList.size() > 0){
Address address = (Address) addressList.get(0);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(address.getLatitude()).append("\n");
stringBuilder.append(address.getLongitude()).append("\n");
result = stringBuilder.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
Message message = Message.obtain();
message.setTarget(handler);
if (result != null){
message.what = 1;
Bundle bundle = new Bundle();
result = "Address : "+locationAddress+
"\n\n\nLatitude and longitude\n"+result;
bundle.putString("address",result);
message.setData(bundle);
}
message.sendToTarget();
}
}
};
thread.start();
}
}