From 062739ac7b15e97d38d89360c4f782becea68825 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 5 Jan 2021 16:02:48 +0100 Subject: [PATCH] added navigation to location from map --- .../com/a1/nextlocation/data/Location.java | 19 +++++ .../{CurrentRoute.java => StaticData.java} | 14 +++- .../nextlocation/fragments/HomeFragment.java | 70 +++++++++++++------ .../fragments/LocationDetailFragment.java | 16 +++++ 4 files changed, 95 insertions(+), 24 deletions(-) rename app/src/main/java/com/a1/nextlocation/data/{CurrentRoute.java => StaticData.java} (52%) diff --git a/app/src/main/java/com/a1/nextlocation/data/Location.java b/app/src/main/java/com/a1/nextlocation/data/Location.java index a0822fc..d8ecb58 100644 --- a/app/src/main/java/com/a1/nextlocation/data/Location.java +++ b/app/src/main/java/com/a1/nextlocation/data/Location.java @@ -108,6 +108,25 @@ public class Location implements Parcelable { return long1 + "," + lat1; } + public double getDistance(Location other) { + double dlon = other.getLong() - getLong(); + double dlat = other.getLat() - getLong(); + double a = Math.pow(Math.sin(dlat / 2), 2) + + Math.cos(getLat()) * Math.cos(other.getLong()) + * Math.pow(Math.sin(dlon / 2),2); + + double c = 2 * Math.asin(Math.sqrt(a)); + + // Radius of earth in kilometers. Use 3956 + // for miles + double r = 6371; + + // calculate the result + double distance = c * r; + + return Math.floor(distance); + } + public GeoPoint convertToGeoPoint() { return new GeoPoint(this.getLat(),this.getLong()); } diff --git a/app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java b/app/src/main/java/com/a1/nextlocation/data/StaticData.java similarity index 52% rename from app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java rename to app/src/main/java/com/a1/nextlocation/data/StaticData.java index a0642f0..7114803 100644 --- a/app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java +++ b/app/src/main/java/com/a1/nextlocation/data/StaticData.java @@ -2,8 +2,20 @@ package com.a1.nextlocation.data; import org.osmdroid.views.overlay.Polyline; -public enum CurrentRoute { +/** + * singleton to keep track of different global data + */ +public enum StaticData { INSTANCE; + private double distanceTraveled = 0; + + public void addDistance(double d) { + distanceTraveled += d; + } + + public double getDistanceTraveled() { + return distanceTraveled; + } private Polyline currentRoute; diff --git a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java index 4e0db7b..2aedc11 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -5,7 +5,6 @@ package com.a1.nextlocation.fragments; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; -import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.location.Location; import android.location.LocationListener; @@ -26,19 +25,12 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import com.a1.nextlocation.R; -import com.a1.nextlocation.data.CurrentRoute; -import com.a1.nextlocation.data.Route; +import com.a1.nextlocation.data.StaticData; import com.a1.nextlocation.json.DirectionsResult; import com.a1.nextlocation.network.ApiHandler; -import com.a1.nextlocation.recyclerview.CouponListManager; -import com.a1.nextlocation.recyclerview.CustomOverlay; import com.a1.nextlocation.recyclerview.LocationListManager; import org.osmdroid.api.IMapController; -import org.osmdroid.bonuspack.routing.MapQuestRoadManager; -import org.osmdroid.bonuspack.routing.OSRMRoadManager; -import org.osmdroid.bonuspack.routing.Road; -import org.osmdroid.bonuspack.routing.RoadManager; import org.osmdroid.config.Configuration; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapView; @@ -52,11 +44,9 @@ import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.List; -public class HomeFragment extends Fragment { +public class HomeFragment extends Fragment implements LocationListener{ private final String userAgent = "com.ai.nextlocation.fragments"; public final static String MAPQUEST_API_KEY = "vuyXjqnAADpjeL9QwtgWGleIk95e36My"; private ImageButton imageButton; @@ -66,8 +56,7 @@ public class HomeFragment extends Fragment { // private RoadManager roadManager; private Polyline roadOverlay; private int color; - - private GeoPoint currentLocation; + private Location currentLocation; @Override public void onCreate(Bundle savedInstanceState) { @@ -114,7 +103,7 @@ public class HomeFragment extends Fragment { roadOverlay.setColor(color); - CurrentRoute.INSTANCE.setCurrentRoute(roadOverlay); + StaticData.INSTANCE.setCurrentRoute(roadOverlay); Log.d(TAG, "onDirectionsAvailable: successfully added road!"); } @@ -148,6 +137,8 @@ public class HomeFragment extends Fragment { compassOverlay.enableCompass(); mapView.getOverlays().add(compassOverlay); + addLocations(); + // add the location overlay MyLocationNewOverlay mLocationOverlay = new MyLocationNewOverlay(gpsMyLocationProvider, mapView); mLocationOverlay.enableFollowLocation(); @@ -165,9 +156,13 @@ public class HomeFragment extends Fragment { try { Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + if (currentLocation == null) { + currentLocation = location; + } + if( location != null ) { - currentLocation = new GeoPoint(location.getLatitude(), location.getLongitude()); - mapController.setCenter(currentLocation); + GeoPoint start = new GeoPoint(location.getLatitude(), location.getLongitude()); + mapController.setCenter(start); } } catch (SecurityException e) { @@ -183,15 +178,14 @@ public class HomeFragment extends Fragment { displayRoute(); - addLocations(); } private void displayRoute() { if (roadOverlay == null) { - if (CurrentRoute.INSTANCE.getCurrentRoute() != null) { - roadOverlay = CurrentRoute.INSTANCE.getCurrentRoute(); + if (StaticData.INSTANCE.getCurrentRoute() != null) { + roadOverlay = StaticData.INSTANCE.getCurrentRoute(); mapView.getOverlays().add(roadOverlay); mapView.invalidate(); Log.d(TAG, "initMap: successfully added road!"); @@ -206,7 +200,9 @@ public class HomeFragment extends Fragment { private void addLocations() { List locations = LocationListManager.INSTANCE.getLocationList(); final ArrayList items = new ArrayList<>(locations.size()); - Drawable marker = this.getResources().getDrawable(R.drawable.ic_baseline_location_on_24); + Drawable marker = ContextCompat.getDrawable(requireContext(),R.drawable.ic_baseline_location_on_24); + marker.setAlpha(255); + marker.setTint(getResources().getColor(R.color.primaryColour)); for (com.a1.nextlocation.data.Location location : locations) { OverlayItem item = new OverlayItem(location.getName(),location.getDescription(),location.convertToGeoPoint()); item.setMarker(marker); @@ -216,13 +212,19 @@ public class HomeFragment extends Fragment { new ItemizedIconOverlay.OnItemGestureListener() { @Override public boolean onItemSingleTapUp(int index, OverlayItem item) { - + com.a1.nextlocation.data.Location clicked = locations.get(index); + requireActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new LocationDetailFragment(clicked)).commit(); return false; } @Override public boolean onItemLongPress(int index, OverlayItem item) { - Toast.makeText(requireContext(), locations.get(index).getName(),Toast.LENGTH_SHORT).show(); + com.a1.nextlocation.data.Location clicked = locations.get(index); + Toast.makeText(requireContext(), clicked.getName(),Toast.LENGTH_SHORT).show(); +// Route route = new Route("Route to " + clicked.getName()); +// route.addLocation(new com.a1.nextlocation.data.Location("Current location",currentLocation.getLatitude(),currentLocation.getLongitude(),"your location",null)); +// route.addLocation(clicked); +// ApiHandler.INSTANCE.getDirections(route); return true; } },requireContext()); @@ -249,4 +251,26 @@ public class HomeFragment extends Fragment { REQUEST_PERMISSIONS_REQUEST_CODE); } } + + @Override + public void onLocationChanged(@NonNull Location location) { + double distance = currentLocation.distanceTo(location); // in meters + StaticData.INSTANCE.addDistance(distance); + currentLocation = location; + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + + } + + @Override + public void onProviderEnabled(@NonNull String provider) { + + } + + @Override + public void onProviderDisabled(@NonNull String provider) { + + } } \ No newline at end of file diff --git a/app/src/main/java/com/a1/nextlocation/fragments/LocationDetailFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/LocationDetailFragment.java index 82bcabb..19f73cd 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/LocationDetailFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/LocationDetailFragment.java @@ -1,20 +1,33 @@ package com.a1.nextlocation.fragments; +import android.content.Intent; import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import com.a1.nextlocation.R; +import com.a1.nextlocation.data.Location; public class LocationDetailFragment extends Fragment { + private static final String TAG = LocationDetailFragment.class.getCanonicalName(); private ImageButton imageButton; + private Location location; + + public LocationDetailFragment() { + + } + + public LocationDetailFragment(Location location) { + this.location = location; + } @Override public void onCreate(Bundle savedInstanceState) { @@ -31,6 +44,9 @@ public class LocationDetailFragment extends Fragment { ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, locationFragment).addToBackStack(null).commit(); }); + if (location != null) { + Log.d(TAG, "onCreateView: the location has a name of: " + location.getName()); + } return view; } } \ No newline at end of file