Change location beans class.

This commit is contained in:
Geoffrey POUZET 2018-08-14 14:30:38 +02:00
parent 95ad89a181
commit e883f7eeba
4 changed files with 62 additions and 37 deletions

View File

@ -6,7 +6,6 @@ import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.hardware.GeomagneticField; import android.hardware.GeomagneticField;
import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -39,9 +38,12 @@ import org.osmdroid.views.overlay.infowindow.BasicInfoWindow;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; 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.List;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import fr.chteufleur.mytrackingdog.models.beans.MyLocation;
import fr.chteufleur.mytrackingdog.services.ServiceGps; import fr.chteufleur.mytrackingdog.services.ServiceGps;
public class MainActivity extends AppCompatActivity implements IOrientationConsumer, Observer { public class MainActivity extends AppCompatActivity implements IOrientationConsumer, Observer {
@ -180,7 +182,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
private void addMarker() { private void addMarker() {
GeoPoint gp = serviceGps.addPointObject(); MyLocation loc = serviceGps.addPointObject();
GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
Marker marker = new Marker(map); Marker marker = new Marker(map);
marker.setPosition(gp); marker.setPosition(gp);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM); marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
@ -311,7 +314,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
@Override @Override
public void onOrientationChanged(float orientationToMagneticNorth, IOrientationProvider source) { public void onOrientationChanged(float orientationToMagneticNorth, IOrientationProvider source) {
//note, on devices without a compass this never fires... //note, on devices without a compass this never fires...
Location location = serviceGps.getCurrentLocation(); MyLocation location = serviceGps.getCurrentLocation();
if (location == null) { if (location == null) {
return ; return ;
} }
@ -370,7 +373,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
line = new Polyline(map); line = new Polyline(map);
line.setTitle("Traceur"); line.setTitle("Traceur");
line.setColor(Color.RED); line.setColor(Color.RED);
line.setPoints(serviceGps.getListGeoPointTraceur()); line.setPoints(convertListLocation(serviceGps.getListGeoPointTraceur()));
} else if (serviceGps.isDogActivated()) { } else if (serviceGps.isDogActivated()) {
Log.i(TAG, "polyline dog"); Log.i(TAG, "polyline dog");
@ -379,7 +382,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
line = new Polyline(map); line = new Polyline(map);
line.setTitle("Dog"); line.setTitle("Dog");
line.setColor(Color.BLUE); line.setColor(Color.BLUE);
line.setPoints(serviceGps.getListGeoPointDog()); line.setPoints(convertListLocation(serviceGps.getListGeoPointDog()));
} }
if (line != null) { if (line != null) {
@ -397,4 +400,14 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
} }
} }
} }
private List<GeoPoint> convertListLocation(List<MyLocation> list) {
List<GeoPoint> ret = new ArrayList<>();
if (list != null) {
for (MyLocation loc : list) {
ret.add(new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()));
}
}
return ret;
}
} }

View File

@ -2,50 +2,50 @@ package fr.chteufleur.mytrackingdog.models;
import android.util.Log; import android.util.Log;
import org.osmdroid.util.GeoPoint;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import fr.chteufleur.mytrackingdog.models.beans.MyLocation;
public class Traces { public class Traces {
private static final String TAG = Traces.class.getName(); private static final String TAG = Traces.class.getName();
private List<GeoPoint> lGeoPointTraceur = new ArrayList<>(); private List<MyLocation> listPointTraceur = new ArrayList<>();
private List<GeoPoint> lGeoPointDog = new ArrayList<>(); private List<MyLocation> listPointDog = new ArrayList<>();
private List<GeoPoint> lGeoPointObjects = new ArrayList<>(); private List<MyLocation> listPointObjects = new ArrayList<>();
private boolean traceurActivated = false; private boolean traceurActivated = false;
private boolean dogActivated = false; private boolean dogActivated = false;
public void addPointTraceur(GeoPoint point) { public void addPointTraceur(MyLocation point) {
Log.i(TAG, "add point traceur"); Log.i(TAG, "add point traceur");
lGeoPointTraceur.add(point); listPointTraceur.add(point);
} }
public void addPointDog(GeoPoint point) { public void addPointDog(MyLocation point) {
Log.i(TAG, "add point dog"); Log.i(TAG, "add point dog");
lGeoPointDog.add(point); listPointDog.add(point);
} }
public void addPointObject(GeoPoint point) { public void addPointObject(MyLocation point) {
Log.i(TAG, "add point object"); Log.i(TAG, "add point object");
lGeoPointObjects.add(point); listPointObjects.add(point);
} }
public void addCurrentPoint(GeoPoint point) { public void addCurrentPoint(MyLocation point) {
if (traceurActivated) { if (traceurActivated) {
addPointTraceur(point); addPointTraceur(point);
} else if (dogActivated) { } else if (dogActivated) {
addPointDog(point); addPointDog(point);
} }
} }
public List<GeoPoint> getListGeoPointTraceur() { public List<MyLocation> getListPointTraceur() {
return lGeoPointTraceur; return listPointTraceur;
} }
public List<GeoPoint> getListGeoPointDog() { public List<MyLocation> getListPointDog() {
return lGeoPointDog; return listPointDog;
} }
public List<GeoPoint> getListGeoPointObjects() { public List<MyLocation> getListPointObjects() {
return lGeoPointObjects; return listPointObjects;
} }
public void toggleTraceurActivation() { public void toggleTraceurActivation() {

View File

@ -0,0 +1,10 @@
package fr.chteufleur.mytrackingdog.models.beans;
import android.location.Location;
public class MyLocation extends Location {
public MyLocation(Location l) {
super(l);
}
}

View File

@ -12,6 +12,7 @@ import java.util.List;
import java.util.Observable; import java.util.Observable;
import fr.chteufleur.mytrackingdog.models.Traces; import fr.chteufleur.mytrackingdog.models.Traces;
import fr.chteufleur.mytrackingdog.models.beans.MyLocation;
public class ServiceGps extends Observable implements IServiceGps, LocationListener { public class ServiceGps extends Observable implements IServiceGps, LocationListener {
@ -19,7 +20,7 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
public static final String NOTIF_NEW_LOCATION = "fr.chteufleur.mytrackingdog.services.servicegps.newlocation"; public static final String NOTIF_NEW_LOCATION = "fr.chteufleur.mytrackingdog.services.servicegps.newlocation";
private LocationManager locationManager; private LocationManager locationManager;
private Location currentLocation = null; private MyLocation currentLocation = null;
private final Traces traces = new Traces(); private final Traces traces = new Traces();
@ -27,7 +28,7 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
this.locationManager = locationManager; this.locationManager = locationManager;
} }
public Location getCurrentLocation() { public MyLocation getCurrentLocation() {
return currentLocation; return currentLocation;
} }
@ -67,8 +68,8 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
currentLocation = location; currentLocation = new MyLocation(location);
traces.addCurrentPoint(getCurrentGeoPoint()); traces.addCurrentPoint(currentLocation);
setChanged(); setChanged();
notifyObservers(NOTIF_NEW_LOCATION); notifyObservers(NOTIF_NEW_LOCATION);
} }
@ -109,19 +110,20 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
return traces.isDogActivated(); return traces.isDogActivated();
} }
public GeoPoint addPointObject() { public MyLocation addPointObject() {
GeoPoint point = getCurrentGeoPoint(); if (currentLocation != null) {
traces.addPointObject(point); traces.addPointObject(currentLocation);
return point; }
return currentLocation;
} }
public List<GeoPoint> getListGeoPointTraceur() { public List<MyLocation> getListGeoPointTraceur() {
return traces.getListGeoPointTraceur(); return traces.getListPointTraceur();
} }
public List<GeoPoint> getListGeoPointDog() { public List<MyLocation> getListGeoPointDog() {
return traces.getListGeoPointDog(); return traces.getListPointDog();
} }
public List<GeoPoint> getListGeoPointObjects() { public List<MyLocation> getListGeoPointObjects() {
return traces.getListGeoPointObjects(); return traces.getListPointObjects();
} }
} }