0% found this document useful (0 votes)
31 views3 pages

Android Location Tracking with Maps

The document outlines an Android application that utilizes location services and Google Maps. It includes permissions for internet and location access, sets up a GoogleApiClient for location updates, and displays the user's location on a map with markers. The app also provides functionality to fetch and display directions between two points using the Google Maps Directions API.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Android Location Tracking with Maps

The document outlines an Android application that utilizes location services and Google Maps. It includes permissions for internet and location access, sets up a GoogleApiClient for location updates, and displays the user's location on a map with markers. The app also provides functionality to fetch and display directions between two points using the Google Maps Directions API.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

AndroidManifest.

xml

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


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

[Link]

public class MainActivity extends AppCompatActivity implements


OnMapReadyCallback, LocationListener, [Link],
[Link], TaskLoadedCallback {

private GoogleMap mMap;


private MarkerOptions place1, place2;
private Polyline currentPolyline;
private boolean isFirstTime = true;

private TextView tvLocation;


private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

private final long UPDATE_INTERVAL = 2000;


private final long FASTEST_INTERVAL = 2000;

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

requestLocationPermissions();

mGoogleApiClient = new [Link](this)


.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi([Link])
.build();
}

private void requestLocationPermissions() {


[Link](this)
.withPermissions([Link].ACCESS_FINE_LOCATION,
[Link].ACCESS_COARSE_LOCATION)
.withListener(new MultiplePermissionsListener() {
public void onPermissionsChecked(MultiplePermissionsReport
report) {
if ([Link]())
openSettingsDialog();
}
public void
onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions,
PermissionToken token) {
[Link]();
}
}).check();
}

private void openSettingsDialog() {


new [Link](this)
.setTitle("Permission Required")
.setMessage("Grant location permission in settings.")
.setPositiveButton("Settings", (d, w) -> {
Intent intent = new
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
[Link]([Link]("package", getPackageName(),
null));
startActivity(intent);
})
.setNegativeButton("Cancel", null)
.show();
}

@Override protected void onStart() { [Link](); if (mGoogleApiClient !=


null) [Link](); }
@Override protected void onStop() { [Link](); if
([Link]()) [Link](); }

@Override public void onConnected(Bundle bundle) {


if ([Link](this,
[Link].ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
return;
startLocationUpdates();
Location location =
[Link](mGoogleApiClient);
if (location != null) onLocationChanged(location);
else [Link](this, "Location not detected",
Toast.LENGTH_SHORT).show();
}

private void startLocationUpdates() {


mLocationRequest = [Link]()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL);

if ([Link](this,
[Link].ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
return;

[Link](mGoogleApiClient,
mLocationRequest, this);
}

@Override public void onLocationChanged(Location location) {


[Link]([Link]() + ", " +
[Link]());

if (isFirstTime) {
place1 = new MarkerOptions().position(new
LatLng([Link](), [Link]())).title("You");
place2 = new MarkerOptions().position(new LatLng(19.021824,
72.8662016)).title("MSBTE");

((MapFragment)
getFragmentManager().findFragmentById([Link])).getMapAsync(this);

findViewById([Link]).setOnClickListener(v ->
new FetchURL(this).execute(getUrl([Link](),
[Link](), "driving"), "driving")
);
isFirstTime = false;
}
}

private String getUrl(LatLng origin, LatLng dest, String mode) {


return "[Link] +
"origin=" + [Link] + "," + [Link] +
"&destination=" + [Link] + "," + [Link] +
"&mode=" + mode +
"&key=" + getString([Link].google_maps_key);
}

@Override public void onMapReady(GoogleMap googleMap) {


mMap = googleMap;
[Link]();
[Link](place1);
[Link](place2);
[Link]([Link](
new
[Link]().target([Link]()).zoom(12).build()
));
}

@Override public void onTaskDone(Object... values) {


if (currentPolyline != null) [Link]();
currentPolyline = [Link]((PolylineOptions) values[0]);
}

@Override public void onConnectionSuspended(int i)


{ [Link](); }
@Override public void onConnectionFailed(ConnectionResult result) {}
}

You might also like