Geocoding and Reversegeocoding
Geocoding and Reversegeocoding
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/etAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter address"/>
<Button
android:id="@+id/btnGeocode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Geocode"/>
<EditText
android:id="@+id/etLatitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter latitude"/>
<EditText
android:id="@+id/etLongitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter longitude"/>
<Button
android:id="@+id/btnReverseGeocode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reverse Geocode"/>
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"/>
</LinearLayout>
Mainactivuty.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.location.Address;
import android.location.Geocoder;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etAddress = findViewById(R.id.etAddress);
etLatitude = findViewById(R.id.etLatitude);
etLongitude = findViewById(R.id.etLongitude);
tvResult = findViewById(R.id.tvResult);
Button btnGeocode = findViewById(R.id.btnGeocode);
Button btnReverseGeocode = findViewById(R.id.btnReverseGeocode);
btnGeocode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
geocodeLocation(etAddress.getText().toString());
}
});
btnReverseGeocode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double latitude =
Double.parseDouble(etLatitude.getText().toString());
double longitude =
Double.parseDouble(etLongitude.getText().toString());
reverseGeocodeLocation(latitude, longitude);
}
});
}