0% found this document useful (0 votes)
34 views

Geocoding and Reversegeocoding

Uploaded by

zyaaryan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Geocoding and Reversegeocoding

Uploaded by

zyaaryan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?xml version="1.0" encoding="utf-8"?

>
<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;

public class MainActivity extends AppCompatActivity {

private EditText etAddress, etLatitude, etLongitude;


private TextView tvResult;

@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);
}
});
}

private void geocodeLocation(String locationName) {


Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocationName(locationName,
1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
tvResult.setText("Latitude: " + address.getLatitude() + "\
nLongitude: " + address.getLongitude());
} else {
tvResult.setText("No location found");
}
} catch (IOException e) {
e.printStackTrace();
}
}

private void reverseGeocodeLocation(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);
tvResult.setText("Address: " + address.getAddressLine(0));
} else {
tvResult.setText("No address found");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy