location stuff

This commit is contained in:
Sem van der Hoeven
2021-01-05 12:13:42 +01:00
parent e8af38981f
commit f5f077db0f
3 changed files with 59 additions and 29 deletions

View File

@@ -7,6 +7,7 @@ import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
@@ -53,7 +54,7 @@ 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;
@@ -62,6 +63,9 @@ public class HomeFragment extends Fragment {
private final String TAG = HomeFragment.class.getCanonicalName();
private RoadManager roadManager;
private Polyline roadOverlay;
private int color;
private GeoPoint currentLocation;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -71,8 +75,10 @@ public class HomeFragment extends Fragment {
Manifest.permission.ACCESS_FINE_LOCATION,
// WRITE_EXTERNAL_STORAGE is required in order to show the map
Manifest.permission.WRITE_EXTERNAL_STORAGE);
roadManager = new OSRMRoadManager(requireContext());
roadManager = new MapQuestRoadManager(MAPQUEST_API_KEY);
roadManager.addRequestOption("routeType=foot-walking");
color = requireContext().getColor(R.color.red);
}
@Override
@@ -98,7 +104,14 @@ public class HomeFragment extends Fragment {
ArrayList<GeoPoint> geoPoints = directionsResult.getGeoPoints();
roadOverlay = new Polyline();
roadOverlay.setPoints(geoPoints);
roadOverlay.setColor(R.color.red);
// this is for mapquest, but it gives a "no value for guidancelinkcollection" error and google has never heard of that
// GeoPoint[] gp = directionsResult.getStartAndEndPoint();
// ArrayList<GeoPoint> arrayList = new ArrayList<>(Arrays.asList(gp));
// Road road = roadManager.getRoad(arrayList);
// roadOverlay = RoadManager.buildRoadOverlay(road);
roadOverlay.setColor(color);
CurrentRoute.INSTANCE.setCurrentRoute(roadOverlay);
@@ -137,19 +150,6 @@ public class HomeFragment extends Fragment {
mLocationOverlay.enableMyLocation();
mapView.getOverlays().add(mLocationOverlay);
// CustomOverlay customOverlay = new CustomOverlay(getResources().getDrawable(R.drawable.ic_baseline_location_on_24),mapView);
//
// for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) {
// GeoPoint p = new GeoPoint(l.getLat(), l.getLong());
// OverlayItem overlayItem = new OverlayItem(l.getName(),l.getDescription(), p);
//
// customOverlay.addOverlayItem(overlayItem);
// Log.d(TAG, "initMap: " + "succes");
// }
// mapView.getOverlays().add(customOverlay);
// add the zoom controller
IMapController mapController = mapView.getController();
mapController.setZoom(15.0);
@@ -158,10 +158,15 @@ public class HomeFragment extends Fragment {
LocationManager locationManager = (LocationManager) requireActivity().getSystemService(Context.LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
GeoPoint startPoint = new GeoPoint(location.getLatitude(), location.getLongitude());
mapController.setCenter(startPoint);
if( location != null ) {
currentLocation = new GeoPoint(location.getLatitude(), location.getLongitude());
mapController.setCenter(currentLocation);
}
} catch (SecurityException e) {
Log.d(TAG, "onViewCreated: exception while getting location: " + e.getLocalizedMessage());
@@ -205,4 +210,28 @@ public class HomeFragment extends Fragment {
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
@Override
public void onLocationChanged(@NonNull Location location) {
currentLocation = new GeoPoint(location);
IMapController mapController = mapView.getController();
mapController.animateTo(new GeoPoint(location.getLatitude(),location.getLongitude()));
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
// this is deprecated but android is stupid and gives an error if I don't override it
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}

View File

@@ -70,13 +70,14 @@ public class RouteFragment extends Fragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ApiHandler.INSTANCE.getDirections(51.49017262451581, 4.289038164073164,51.47337383133509, 4.303535222390562);
// ApiHandler.INSTANCE.getDirections(51.49017262451581, 4.289038164073164,51.47337383133509, 4.303535222390562);
Route r = new Route("test");
r.addLocation(new Location("test",51.487963606716185, 4.280396603442448,"route",null));
r.addLocation(new Location("test",51.486962126272886, 4.27884897122147,"route",null));
r.addLocation(new Location("test",51.485412622620224, 4.277392376189963,"route",null));
r.addLocation(new Location("test",51.4801212376542, 4.281610432511638,"route",null));
r.addLocation(new Location("test",51.481103969835665, 4.2903500027691805,"route",null));
r.addLocation(new Location("test",51.574473766034046, 4.7628379328055175,"route",null));
r.addLocation(new Location("test",51.577354223919876, 4.771120593941968,"route",null));
r.addLocation(new Location("test",51.573033468635174, 4.782750651807139,"route",null));
r.addLocation(new Location("test",51.56519104881196, 4.748246716295709,"route",null));
r.addLocation(new Location("test",51.57367360644676, 4.74404101271347,"route",null));
r.addLocation(new Location("test",51.57852769146427, 4.739878224473907,"route",null));
// r.addLocation(new Location("test",51.489063681658145, 4.289596063527951,"route",null));
// r.addLocation(new Location("test",51.483012677667766, 4.28003245468457,"route",null));
ApiHandler.INSTANCE.getDirections(r);

View File

@@ -131,11 +131,11 @@ public enum ApiHandler {
t.start();
// try {
// t.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}