added navigation to location from map
This commit is contained in:
@@ -108,6 +108,25 @@ public class Location implements Parcelable {
|
|||||||
return long1 + "," + lat1;
|
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() {
|
public GeoPoint convertToGeoPoint() {
|
||||||
return new GeoPoint(this.getLat(),this.getLong());
|
return new GeoPoint(this.getLat(),this.getLong());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,20 @@ package com.a1.nextlocation.data;
|
|||||||
|
|
||||||
import org.osmdroid.views.overlay.Polyline;
|
import org.osmdroid.views.overlay.Polyline;
|
||||||
|
|
||||||
public enum CurrentRoute {
|
/**
|
||||||
|
* singleton to keep track of different global data
|
||||||
|
*/
|
||||||
|
public enum StaticData {
|
||||||
INSTANCE;
|
INSTANCE;
|
||||||
|
private double distanceTraveled = 0;
|
||||||
|
|
||||||
|
public void addDistance(double d) {
|
||||||
|
distanceTraveled += d;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getDistanceTraveled() {
|
||||||
|
return distanceTraveled;
|
||||||
|
}
|
||||||
|
|
||||||
private Polyline currentRoute;
|
private Polyline currentRoute;
|
||||||
|
|
||||||
@@ -5,7 +5,6 @@ package com.a1.nextlocation.fragments;
|
|||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.res.Resources;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
@@ -26,19 +25,12 @@ import androidx.fragment.app.Fragment;
|
|||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
|
||||||
import com.a1.nextlocation.R;
|
import com.a1.nextlocation.R;
|
||||||
import com.a1.nextlocation.data.CurrentRoute;
|
import com.a1.nextlocation.data.StaticData;
|
||||||
import com.a1.nextlocation.data.Route;
|
|
||||||
import com.a1.nextlocation.json.DirectionsResult;
|
import com.a1.nextlocation.json.DirectionsResult;
|
||||||
import com.a1.nextlocation.network.ApiHandler;
|
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 com.a1.nextlocation.recyclerview.LocationListManager;
|
||||||
|
|
||||||
import org.osmdroid.api.IMapController;
|
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.config.Configuration;
|
||||||
import org.osmdroid.util.GeoPoint;
|
import org.osmdroid.util.GeoPoint;
|
||||||
import org.osmdroid.views.MapView;
|
import org.osmdroid.views.MapView;
|
||||||
@@ -52,11 +44,9 @@ import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
|
|||||||
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
|
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class HomeFragment extends Fragment {
|
public class HomeFragment extends Fragment implements LocationListener{
|
||||||
private final String userAgent = "com.ai.nextlocation.fragments";
|
private final String userAgent = "com.ai.nextlocation.fragments";
|
||||||
public final static String MAPQUEST_API_KEY = "vuyXjqnAADpjeL9QwtgWGleIk95e36My";
|
public final static String MAPQUEST_API_KEY = "vuyXjqnAADpjeL9QwtgWGleIk95e36My";
|
||||||
private ImageButton imageButton;
|
private ImageButton imageButton;
|
||||||
@@ -66,8 +56,7 @@ public class HomeFragment extends Fragment {
|
|||||||
// private RoadManager roadManager;
|
// private RoadManager roadManager;
|
||||||
private Polyline roadOverlay;
|
private Polyline roadOverlay;
|
||||||
private int color;
|
private int color;
|
||||||
|
private Location currentLocation;
|
||||||
private GeoPoint currentLocation;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -114,7 +103,7 @@ public class HomeFragment extends Fragment {
|
|||||||
roadOverlay.setColor(color);
|
roadOverlay.setColor(color);
|
||||||
|
|
||||||
|
|
||||||
CurrentRoute.INSTANCE.setCurrentRoute(roadOverlay);
|
StaticData.INSTANCE.setCurrentRoute(roadOverlay);
|
||||||
Log.d(TAG, "onDirectionsAvailable: successfully added road!");
|
Log.d(TAG, "onDirectionsAvailable: successfully added road!");
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -148,6 +137,8 @@ public class HomeFragment extends Fragment {
|
|||||||
compassOverlay.enableCompass();
|
compassOverlay.enableCompass();
|
||||||
mapView.getOverlays().add(compassOverlay);
|
mapView.getOverlays().add(compassOverlay);
|
||||||
|
|
||||||
|
addLocations();
|
||||||
|
|
||||||
// add the location overlay
|
// add the location overlay
|
||||||
MyLocationNewOverlay mLocationOverlay = new MyLocationNewOverlay(gpsMyLocationProvider, mapView);
|
MyLocationNewOverlay mLocationOverlay = new MyLocationNewOverlay(gpsMyLocationProvider, mapView);
|
||||||
mLocationOverlay.enableFollowLocation();
|
mLocationOverlay.enableFollowLocation();
|
||||||
@@ -165,9 +156,13 @@ public class HomeFragment extends Fragment {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||||
|
if (currentLocation == null) {
|
||||||
|
currentLocation = location;
|
||||||
|
}
|
||||||
|
|
||||||
if( location != null ) {
|
if( location != null ) {
|
||||||
currentLocation = new GeoPoint(location.getLatitude(), location.getLongitude());
|
GeoPoint start = new GeoPoint(location.getLatitude(), location.getLongitude());
|
||||||
mapController.setCenter(currentLocation);
|
mapController.setCenter(start);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SecurityException e) {
|
} catch (SecurityException e) {
|
||||||
@@ -183,15 +178,14 @@ public class HomeFragment extends Fragment {
|
|||||||
|
|
||||||
|
|
||||||
displayRoute();
|
displayRoute();
|
||||||
addLocations();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displayRoute() {
|
private void displayRoute() {
|
||||||
|
|
||||||
if (roadOverlay == null) {
|
if (roadOverlay == null) {
|
||||||
if (CurrentRoute.INSTANCE.getCurrentRoute() != null) {
|
if (StaticData.INSTANCE.getCurrentRoute() != null) {
|
||||||
roadOverlay = CurrentRoute.INSTANCE.getCurrentRoute();
|
roadOverlay = StaticData.INSTANCE.getCurrentRoute();
|
||||||
mapView.getOverlays().add(roadOverlay);
|
mapView.getOverlays().add(roadOverlay);
|
||||||
mapView.invalidate();
|
mapView.invalidate();
|
||||||
Log.d(TAG, "initMap: successfully added road!");
|
Log.d(TAG, "initMap: successfully added road!");
|
||||||
@@ -206,7 +200,9 @@ public class HomeFragment extends Fragment {
|
|||||||
private void addLocations() {
|
private void addLocations() {
|
||||||
List<com.a1.nextlocation.data.Location> locations = LocationListManager.INSTANCE.getLocationList();
|
List<com.a1.nextlocation.data.Location> locations = LocationListManager.INSTANCE.getLocationList();
|
||||||
final ArrayList<OverlayItem> items = new ArrayList<>(locations.size());
|
final ArrayList<OverlayItem> 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) {
|
for (com.a1.nextlocation.data.Location location : locations) {
|
||||||
OverlayItem item = new OverlayItem(location.getName(),location.getDescription(),location.convertToGeoPoint());
|
OverlayItem item = new OverlayItem(location.getName(),location.getDescription(),location.convertToGeoPoint());
|
||||||
item.setMarker(marker);
|
item.setMarker(marker);
|
||||||
@@ -216,13 +212,19 @@ public class HomeFragment extends Fragment {
|
|||||||
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
|
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onItemSingleTapUp(int index, OverlayItem item) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onItemLongPress(int index, OverlayItem item) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
},requireContext());
|
},requireContext());
|
||||||
@@ -249,4 +251,26 @@ public class HomeFragment extends Fragment {
|
|||||||
REQUEST_PERMISSIONS_REQUEST_CODE);
|
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) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,33 @@
|
|||||||
package com.a1.nextlocation.fragments;
|
package com.a1.nextlocation.fragments;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
|
|
||||||
import com.a1.nextlocation.R;
|
import com.a1.nextlocation.R;
|
||||||
|
import com.a1.nextlocation.data.Location;
|
||||||
|
|
||||||
public class LocationDetailFragment extends Fragment {
|
public class LocationDetailFragment extends Fragment {
|
||||||
|
private static final String TAG = LocationDetailFragment.class.getCanonicalName();
|
||||||
|
|
||||||
private ImageButton imageButton;
|
private ImageButton imageButton;
|
||||||
|
private Location location;
|
||||||
|
|
||||||
|
public LocationDetailFragment() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocationDetailFragment(Location location) {
|
||||||
|
this.location = location;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
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();
|
((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;
|
return view;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user