0% found this document useful (0 votes)
40 views4 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Geocoding code

You need to add the necessary permissions in the [Link] file to access the internet
and use Google Maps services:

<uses-permission android:name="[Link]" />

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

1. XML Code

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


<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
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 [Link];
import [Link];
import [Link];
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 {

private EditText editTextAddress;


private Button buttonGeocode;
private TextView textViewResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editTextAddress = findViewById([Link]);
buttonGeocode = findViewById([Link]);
textViewResult = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String address = [Link]().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 =
"[Link] + address +
"&key=" + apiKey;

try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection)
[Link]();
[Link]();
InputStream inputStream = [Link]();
Scanner scanner = new Scanner(inputStream);
StringBuilder stringBuilder = new StringBuilder();
while ([Link]()) {
[Link]([Link]());
}
return [Link]();
} catch (IOException e) {
[Link]();
return null;
}
}

@Override
protected void onPostExecute(String result) {
if (result != null) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject location = [Link]("results")
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location");
double latitude = [Link]("lat");
double longitude = [Link]("lng");
[Link]("Latitude: " + latitude + "\nLongitude: " +
longitude);
} catch (JSONException e) {
[Link]();
}
} else {
[Link]("Error occurred while geocoding.");
}
}
}
}

You might also like