Geocoding and Reverse geocoding
Xml file -:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText editTextAddress;
TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
editTextAddress = findViewById([Link]);
textViewResult = findViewById([Link]);
Button buttonGeocode = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String addressString = [Link]().toString();
if (![Link]()) {
geocodeAddress(addressString);
} else {
[Link]("Please enter an address");
}
}
});
Button buttonReverseGeocode = findViewById([Link]);
[Link](new [Link]() {
@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, [Link]());
try {
List<Address> addresses = [Link](addressString, 1);
if (addresses != null && ![Link]()) {
Address address = [Link](0);
String result = "Latitude: " + [Link]() + "\nLongitude: " +
[Link]();
[Link](result);
} else {
[Link]("Address not found");
}
} catch (IOException e) {
[Link]();
}
}
private void reverseGeocode(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(this, [Link]());
try {
List<Address> addresses = [Link](latitude, longitude, 1);
if (addresses != null && ![Link]()) {
Address address = [Link](0);
String result = "Address: " + [Link](0);
[Link](result);
} else {
[Link]("Address not found");
}
} catch (IOException e) {
[Link]();
}
}
}
Manifest file -:
<manifest xmlns:android="[Link]
package="[Link]">
<uses-permission android:name="[Link]" />
<uses-permission android:name="[Link].ACCESS_FINE_LOCATION" />
<uses-permission android:name="[Link].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="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>