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

Location and Geocoding Code

The documents contain two Android Java code examples for location services. The first code retrieves the current location using the Fused Location Provider and displays latitude and longitude, while the second code demonstrates geocoding and reverse geocoding functionalities using the Geocoder class. Both examples require location permissions and handle user interactions through buttons.

Uploaded by

PALLAVI KAKADE
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)
11 views4 pages

Location and Geocoding Code

The documents contain two Android Java code examples for location services. The first code retrieves the current location using the Fused Location Provider and displays latitude and longitude, while the second code demonstrates geocoding and reverse geocoding functionalities using the Geocoder class. Both examples require location permissions and handle user interactions through buttons.

Uploaded by

PALLAVI KAKADE
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

Code 1: Current Location (MainActivity.

java)

package [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];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_LOCATION_PERMISSION = 1;

TextView locationText;
Button getLocationButton;

FusedLocationProviderClient fusedLocationClient;

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

locationText = findViewById([Link]);
getLocationButton = findViewById([Link]);

fusedLocationClient = [Link](this);

[Link](new [Link]() {
@Override
public void onClick(View v) {
getLastLocation();
}
});
}

private void getLastLocation() {


if ([Link](this, [Link].ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
[Link](this, new String[]{[Link].ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
return;
}

[Link]().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if ([Link]() && [Link]() != null) {
Location location = [Link]();
String result = "Latitude: " + [Link]() + "\nLongitude: " + [Link]();
[Link](result);
Log.d("Location", result);
} else {
[Link]([Link], "Failed to get location", Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
grantResults) {
[Link](requestCode, permissions, grantResults);
if (requestCode == REQUEST_LOCATION_PERMISSION) {
if ([Link] > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLastLocation();
} else {
[Link](this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
}
Code 2: Geocoding Example ([Link])

package [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];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

TextView resultText;
Button geocodeButton, reverseGeocodeButton;

private static final int REQUEST_LOCATION_PERMISSION = 1;

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

resultText = findViewById([Link]);
geocodeButton = findViewById([Link]);
reverseGeocodeButton = findViewById([Link]);

// Request location permission


if ([Link](this, [Link].ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
[Link](this, new String[]{[Link].ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
}

[Link](new [Link]() {
@Override
public void onClick(View v) {
doGeocoding();
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
doReverseGeocoding();
}
});
}

private void doGeocoding() {


Geocoder geocoder = new Geocoder(this, [Link]());
try {
List<Address> addresses = [Link]("Statue of Liberty, New York", 1);
if (addresses != null && [Link]() > 0) {
double latitude = [Link](0).getLatitude();
double longitude = [Link](0).getLongitude();
String result = "Latitude: " + latitude + "\nLongitude: " + longitude;
[Link](result);
Log.d("Geocoding", result);
} else {
[Link](this, "No result found", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
[Link]();
}
}

private void doReverseGeocoding() {


Geocoder geocoder = new Geocoder(this, [Link]());
try {
double latitude = 48.8584; // Eiffel Tower
double longitude = 2.2945;
List<Address> addresses = [Link](latitude, longitude, 1);
if (addresses != null && [Link]() > 0) {
String address = [Link](0).getAddressLine(0);
[Link]("Address: \n" + address);
Log.d("ReverseGeocoding", address);
} else {
[Link](this, "No address found", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
[Link]();
}
}
}

You might also like