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

Geocode

The document describes how to perform geocoding by getting latitude and longitude from an address using the Google Maps Geocoding API. It includes XML layout code for an address input, button and results text view. It also includes Java code to make an API request asynchronously on button click and handle the response.

Uploaded by

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

Geocode

The document describes how to perform geocoding by getting latitude and longitude from an address using the Google Maps Geocoding API. It includes XML layout code for an address input, button and results text view. It also includes Java code to make an API request asynchronously on button click and handle the response.

Uploaded by

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

Geocoding code

You need to add the necessary permissions in the AndroidManifest.xml file to access the internet
and use Google Maps services:

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

1. XML Code

<?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"
tools:context=".MainActivity">

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

public class MainActivity extends AppCompatActivity {

private EditText editTextAddress;


private Button buttonGeocode;
private TextView textViewResult;

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

You might also like