Add trace on map.
This commit is contained in:
parent
f33930e875
commit
cc9fe02195
|
|
@ -4,6 +4,7 @@ import android.Manifest;
|
|||
import android.content.Context;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.hardware.GeomagneticField;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
|
|
@ -11,7 +12,10 @@ import android.os.Build;
|
|||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Surface;
|
||||
|
|
@ -25,13 +29,17 @@ import org.osmdroid.config.Configuration;
|
|||
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
|
||||
import org.osmdroid.util.GeoPoint;
|
||||
import org.osmdroid.views.MapView;
|
||||
import org.osmdroid.views.overlay.Polyline;
|
||||
import org.osmdroid.views.overlay.compass.CompassOverlay;
|
||||
import org.osmdroid.views.overlay.compass.IOrientationConsumer;
|
||||
import org.osmdroid.views.overlay.compass.IOrientationProvider;
|
||||
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider;
|
||||
import org.osmdroid.views.overlay.infowindow.BasicInfoWindow;
|
||||
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
|
||||
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
|
|
@ -57,6 +65,13 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
|
||||
private final int REQUEST_CODE_ASK_PERMISSION = 123;
|
||||
|
||||
private static final float LINE_WIDTH_BIG = 12;
|
||||
private static final int COLOR_POLYLINE_STATIC = Color.RED;
|
||||
List<GeoPoint> lGeoPoint = new ArrayList<>();
|
||||
|
||||
private FloatingActionButton start_stop_trace;
|
||||
private boolean traceurActivated = false;
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
protected void checkPermissions() {
|
||||
int hasLocationPermission = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
|
||||
|
|
@ -136,12 +151,29 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
});
|
||||
|
||||
start_stop_trace = (FloatingActionButton) findViewById(R.id.start_stop_trace);
|
||||
start_stop_trace.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
changeStatusTrace();
|
||||
}
|
||||
});
|
||||
|
||||
if (serviceGps == null) {
|
||||
serviceGps = new ServiceGps();
|
||||
((Observable) serviceGps).addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void changeStatusTrace() {
|
||||
this.traceurActivated = !this.traceurActivated;
|
||||
if (this.traceurActivated) {
|
||||
this.start_stop_trace.setImageResource(R.drawable.ic_menu_offline);
|
||||
} else {
|
||||
this.start_stop_trace.setImageResource(R.drawable.ic_menu_mylocation);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume(){
|
||||
super.onResume();
|
||||
|
|
@ -314,7 +346,22 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
@Override
|
||||
public void update(Observable observable, Object o) {
|
||||
if (observable == serviceGps) {
|
||||
if (o instanceof String && (String) o == ServiceGps.NOTIF_NEW_LOCATION) {
|
||||
if (o instanceof String && (String) o == ServiceGps.NOTIF_NEW_LOCATION && traceurActivated) {
|
||||
lGeoPoint.add(((ServiceGps) serviceGps).getCurrentGeoPoint());
|
||||
map.getController().setCenter(((ServiceGps) serviceGps).getCurrentGeoPoint());
|
||||
|
||||
Polyline line = new Polyline(map);
|
||||
line.setTitle("Traceur");
|
||||
line.setSubDescription(Polyline.class.getCanonicalName());
|
||||
line.setWidth(LINE_WIDTH_BIG);
|
||||
line.setColor(COLOR_POLYLINE_STATIC);
|
||||
line.setPoints(lGeoPoint);
|
||||
line.setGeodesic(true);
|
||||
line.setInfoWindow(new BasicInfoWindow(R.layout.bonuspack_bubble, map));
|
||||
map.getOverlayManager().add(line);
|
||||
map.invalidate();
|
||||
|
||||
|
||||
float orientation = ((ServiceGps) serviceGps).getOrientation(deviceOrientation);
|
||||
if (orientation >= 0) {
|
||||
map.setMapOrientation(orientation);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import android.location.LocationManager;
|
|||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import org.osmdroid.util.GeoPoint;
|
||||
|
||||
import java.util.Observable;
|
||||
|
||||
import fr.chteufleur.mytrackingdog.MainActivity;
|
||||
|
|
@ -81,4 +83,11 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
|
|||
public void onProviderDisabled(String s) {
|
||||
stop();
|
||||
}
|
||||
|
||||
public GeoPoint getCurrentGeoPoint() {
|
||||
if (currentLocation == null) {
|
||||
return null;
|
||||
}
|
||||
return new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude(), currentLocation.getAltitude());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,31 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/parent_container"
|
||||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<org.osmdroid.views.MapView android:id="@+id/map"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent" />
|
||||
<org.osmdroid.views.MapView android:id="@+id/map"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent" />
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/ic_follow_me"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_gravity="top|end"
|
||||
android:layout_marginTop="11dp"
|
||||
android:background="#00ffffff"
|
||||
android:contentDescription="@string/bt_follow_me_description"
|
||||
android:cropToPadding="true"
|
||||
android:src="@drawable/osm_ic_follow_me_on" />
|
||||
</RelativeLayout>
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/start_stop_trace"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="bottom|start"
|
||||
android:layout_margin="@dimen/fab_margin"
|
||||
app:srcCompat="@android:drawable/ic_media_play" />
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
|
|
|
|||
Loading…
Reference in New Issue