Professional Documents
Culture Documents
Document
Document
gpstracking;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.appindexing.Indexable;
import com.google.maps.android.SphericalUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
//Our Map
private GoogleMap mMap;
private Location currentLocation;
//From -> the first coordinate from where we need to calculate the distance
private double fromLongitude;
private double fromLatitude;
//To -> the second coordinate to where we need to calculate the distance
private double toLongitude;
private double toLatitude;
//Google ApiClient
private GoogleApiClient googleApiClient;
//Our buttons
private Button buttonSetTo;
private Button buttonSetFrom;
private Button buttonCalcDistance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to
be used.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
buttonSetTo.setOnClickListener(this);
buttonSetFrom.setOnClickListener(this);
buttonCalcDistance.setOnClickListener(this);
fusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(MapsActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(MapsActivity.this,
android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE);
return;
}
fetchLastLocation();
}
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));
//Adding the created the marker on the map
mMap.addMarker(markerOptions);
}
@Override
// public Action getAction() {
// return Actions.newView(mMap, longitude);
// }
protected void onStart() {
super.onStart();
/* If you�re logging an action on an item that has already been added to the
index,
you don�t have to add the following update line. See
https://firebase.google.com/docs/app-indexing/android/personal-content#update-
the-index for
adding content to the index */
// FirebaseAppIndex.getInstance().update(getIndexable());
// FirebaseUserActions.getInstance().start(getAction());
}
@Override
protected void onStop() {
// FirebaseUserActions.getInstance().end(getAction());
super.onStop();
}
try {
//Parsing json
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines =
routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<LatLng> list = decodePoly(encodedString);
Polyline line = mMap.addPolyline(new PolylineOptions()
.addAll(list)
.width(20)
.color(Color.RED)
.geodesic(true)
);
}
catch (JSONException e) {
}
}
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
return poly;
}
// @Override
// public void onMapReady(GoogleMap googleMap) {
// mMap = googleMap;
// LatLng latLng = new LatLng(20, 78);
// mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// mMap.setOnMarkerDragListener(this);
// mMap.setOnMapLongClickListener(this);
// }
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResult) {
switch (requestCode) {
case LOCATION_REQUEST_CODE:
if (grantResult.length > 0 && grantResult[0] ==
PackageManager.PERMISSION_GRANTED) {
fetchLastLocation();
} else {
Toast.makeText(MapsActivity.this,"Location permission
missing",Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public void onConnected(Bundle bundle) {
getCurrentLocation();
}
@Override
public void onConnectionSuspended(int i) {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
@Override
public void onMapLongClick(LatLng latLng) {
//Clearing all the markers
mMap.clear();
//Adding a new marker to the current pressed position
mMap.addMarker(new MarkerOptions()
.position(latLng)
.draggable(true));
latitude = latLng.latitude;
longitude = latLng.longitude;
}
@Override
public void onMarkerDragStart(Marker marker) {
@Override
public void onMarkerDrag(Marker marker) {
@Override
public void onMarkerDragEnd(Marker marker) {
//Getting the coordinates
latitude = marker.getPosition().latitude;
longitude = marker.getPosition().longitude;
//Moving the map
moveMap();
}
@Override
public void onClick(View v) {
if(v == buttonSetFrom){
fromLatitude = latitude;
fromLongitude = longitude;
Toast.makeText(this,"From set",Toast.LENGTH_SHORT).show();
}
if(v == buttonSetTo){
toLatitude = latitude;
toLongitude = longitude;
Toast.makeText(this,"To set",Toast.LENGTH_SHORT).show();
}
if(v == buttonCalcDistance){
getDirection();
}
}
}