added updating location when you're close to it

This commit is contained in:
Sem van der Hoeven
2021-01-05 16:39:12 +01:00
parent 062739ac7b
commit 587380fd89
6 changed files with 66 additions and 1 deletions

View File

@@ -82,7 +82,7 @@
"name":"MEZZ Breda Keizerstraat 101",
"coordinates":"51.58394697737321,4.779757901349616",
"description":"4811HL Breda",
"imageUrl":"NULL"
"imageUrl":"mezz_breda"
},
{
"name":"Het Klooster Breda Schorsmolenstraat 13",

View File

@@ -35,6 +35,10 @@ public class Location implements Parcelable {
this(name,getStringFromCoordinates(latCoord,longCoord),description,imageUrl);
}
public Location(String name, android.location.Location loc, String description, String imageUrl) {
this(name,getStringFromCoordinates(loc.getLatitude(),loc.getLongitude()),description,imageUrl);
}
protected Location(Parcel in) {
name = in.readString();
coordinates = in.readString();
@@ -127,6 +131,25 @@ public class Location implements Parcelable {
return Math.floor(distance);
}
public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.pow(Math.sin(dlat / 2), 2)
+ Math.cos(lat1) * Math.cos(lat2)
* 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());
}

View File

@@ -2,21 +2,38 @@ package com.a1.nextlocation.data;
import org.osmdroid.views.overlay.Polyline;
import java.util.ArrayList;
/**
* singleton to keep track of different global data
*/
public enum StaticData {
INSTANCE;
private double distanceTraveled = 0;
private int locationsVisited = 0;
private ArrayList<String> visitedNames = new ArrayList<>();
public void addDistance(double d) {
distanceTraveled += d;
}
public double getDistanceTraveled() {
return distanceTraveled;
}
public void visitLocation(Location location) {
if (!visitedNames.contains(location.getName())) {
locationsVisited++;
visitedNames.add(location.getName());
}
}
public int getLocationsVisited() {
return locationsVisited;
}
private Polyline currentRoute;
public void setCurrentRoute(Polyline currentRoute) {

View File

@@ -154,7 +154,12 @@ public class HomeFragment extends Fragment implements LocationListener{
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLocation == null) {
currentLocation = location;
@@ -257,6 +262,16 @@ public class HomeFragment extends Fragment implements LocationListener{
double distance = currentLocation.distanceTo(location); // in meters
StaticData.INSTANCE.addDistance(distance);
currentLocation = location;
Thread t = new Thread(() -> {
for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) {
if (com.a1.nextlocation.data.Location.getDistance(currentLocation.getLatitude(),currentLocation.getLongitude(),l.getLat(),l.getLong()) < 10) {
StaticData.INSTANCE.visitLocation(l);
}
}
});
t.start();
}
@Override

View File

@@ -16,6 +16,7 @@ import android.widget.Toast;
import com.a1.nextlocation.R;
import com.a1.nextlocation.data.Coupon;
import com.a1.nextlocation.data.StaticData;
import com.a1.nextlocation.recyclerview.CouponAdapter;
import com.a1.nextlocation.recyclerview.CouponListManager;
@@ -36,6 +37,13 @@ public class StatisticFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_statistic, container, false);
TextView distance = view.findViewById(R.id.statistics_km);
TextView locs = view.findViewById(R.id.statistics_locations_visited);
double dist = StaticData.INSTANCE.getDistanceTraveled()/1000;
distance.setText("" + String.format("%.1f",dist) + " km");
locs.setText("" + StaticData.INSTANCE.getLocationsVisited());
this.couponList = CouponListManager.INSTANCE.getCouponList();
CouponAdapter adapter = new CouponAdapter(this.getContext(), this.couponList);
TextView couponNumber = view.findViewById(R.id.couponAmount);

View File

@@ -55,6 +55,7 @@
/>
<TextView
android:id="@+id/statistics_km"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" km"
@@ -103,6 +104,7 @@
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/statistics_locations_visited"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GETAL"