Geocode
Geocode
Geocode
You need to add the necessary permissions in the AndroidManifest.xml file to access the internet
and use Google Maps services:
1. XML Code
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Address"
android:layout_margin="16dp"/>
<Button
android:id="@+id/buttonGeocode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextAddress"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Geocode" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonGeocode"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:textSize="18sp"/>
</RelativeLayout>
2. Java Code-
import android.os.AsyncTask;
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 org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAddress = findViewById(R.id.editTextAddress);
buttonGeocode = findViewById(R.id.buttonGeocode);
textViewResult = findViewById(R.id.textViewResult);
buttonGeocode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String address = editTextAddress.getText().toString();
new GeocodingTask().execute(address);
}
});
}
private class GeocodingTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String address = params[0];
String apiKey = "YOUR_API_KEY"; // Replace with your Google Maps
API key
String apiUrl =
"https://maps.googleapis.com/maps/api/geocode/json?address=" + address +
"&key=" + apiKey;
try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
Scanner scanner = new Scanner(inputStream);
StringBuilder stringBuilder = new StringBuilder();
while (scanner.hasNext()) {
stringBuilder.append(scanner.next());
}
return stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject location = jsonObject.getJSONArray("results")
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location");
double latitude = location.getDouble("lat");
double longitude = location.getDouble("lng");
textViewResult.setText("Latitude: " + latitude + "\nLongitude: " +
longitude);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
textViewResult.setText("Error occurred while geocoding.");
}
}
}
}