Geocoding and Reverse geocoding
Xml file -:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Address" />
<Button
android:id="@+id/buttonGeocode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextAddress"
android:layout_marginTop="16dp"
android:text="Geocode" />
<Button
android:id="@+id/buttonReverseGeocode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonGeocode"
android:layout_marginTop="16dp"
android:text="Reverse Geocode" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonReverseGeocode"
android:layout_marginTop="16dp"
android:textColor="@android:color/black"
android:textSize="18sp" />
</RelativeLayout>
Java file -:
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
EditText editTextAddress;
TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAddress = findViewById(R.id.editTextAddress);
textViewResult = findViewById(R.id.textViewResult);
Button buttonGeocode = findViewById(R.id.buttonGeocode);
buttonGeocode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String addressString = editTextAddress.getText().toString();
if (!addressString.isEmpty()) {
geocodeAddress(addressString);
} else {
textViewResult.setText("Please enter an address");
}
}
});
Button buttonReverseGeocode = findViewById(R.id.buttonReverseGeocode);
buttonReverseGeocode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double latitude = 37.7749; // Example latitude
double longitude = -122.4194; // Example longitude
reverseGeocode(latitude, longitude);
}
});
}
private void geocodeAddress(String addressString) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocationName(addressString, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
String result = "Latitude: " + address.getLatitude() + "\nLongitude: " +
address.getLongitude();
textViewResult.setText(result);
} else {
textViewResult.setText("Address not found");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void reverseGeocode(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
String result = "Address: " + address.getAddressLine(0);
textViewResult.setText(result);
} else {
textViewResult.setText("Address not found");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Manifest file -:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.geocoding">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>