diff --git a/docs/Changelog.md b/docs/Changelog.md index 93d362446..e5b32cbdc 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -2,6 +2,7 @@ ## New since 0.6.0-rc1 +- Location layer [#171](https://github.com/mapsforge/vtm/issues/171) - Minor improvements and bug fixes ## Version 0.6.X diff --git a/vtm-android-example/AndroidManifest.xml b/vtm-android-example/AndroidManifest.xml index a143aa340..3c7e8dab3 100644 --- a/vtm-android-example/AndroidManifest.xml +++ b/vtm-android-example/AndroidManifest.xml @@ -38,6 +38,9 @@ + diff --git a/vtm-android-example/src/org/oscim/android/test/LocationActivity.java b/vtm-android-example/src/org/oscim/android/test/LocationActivity.java new file mode 100644 index 000000000..83e37bc0d --- /dev/null +++ b/vtm-android-example/src/org/oscim/android/test/LocationActivity.java @@ -0,0 +1,87 @@ +/* + * Copyright 2016 devemux86 + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ +package org.oscim.android.test; + +import android.content.Context; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Bundle; + +import org.oscim.core.MapPosition; +import org.oscim.layers.LocationLayer; + +public class LocationActivity extends SimpleMapActivity implements LocationListener { + private LocationLayer locationLayer; + private LocationManager locationManager; + private final MapPosition mapPosition = new MapPosition(); + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); + + locationLayer = new LocationLayer(mMap); + mMap.layers().add(locationLayer); + } + + @Override + protected void onResume() { + super.onResume(); + + enableAvailableProviders(); + } + + @Override + protected void onPause() { + super.onPause(); + + locationManager.removeUpdates(this); + } + + @Override + public void onLocationChanged(Location location) { + locationLayer.setPosition(location.getLatitude(), location.getLongitude(), location.getAccuracy()); + + // Follow location + mMap.getMapPosition(mapPosition); + mapPosition.setPosition(location.getLatitude(), location.getLongitude()); + mMap.setMapPosition(mapPosition); + } + + @Override + public void onProviderDisabled(String provider) { + } + + @Override + public void onProviderEnabled(String provider) { + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + } + + private void enableAvailableProviders() { + locationManager.removeUpdates(this); + + for (String provider : locationManager.getProviders(true)) { + if (LocationManager.GPS_PROVIDER.equals(provider) + || LocationManager.NETWORK_PROVIDER.equals(provider)) { + locationManager.requestLocationUpdates(provider, 0, 0, this); + } + } + } +} diff --git a/vtm-android-example/src/org/oscim/android/test/Samples.java b/vtm-android-example/src/org/oscim/android/test/Samples.java index c1de3d3da..b0d647315 100644 --- a/vtm-android-example/src/org/oscim/android/test/Samples.java +++ b/vtm-android-example/src/org/oscim/android/test/Samples.java @@ -26,14 +26,10 @@ import android.widget.Button; import android.widget.LinearLayout; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * A simple start screen for the sample activities. */ public class Samples extends Activity { - public static Logger log = LoggerFactory.getLogger(Samples.class); @Override protected void onCreate(Bundle savedInstanceState) { @@ -48,6 +44,7 @@ protected void onCreate(Bundle savedInstanceState) { linearLayout.addView(createButton(PathOverlayActivity.class)); linearLayout.addView(createButton(LineTexActivity.class)); linearLayout.addView(createButton(LayerGroupActivity.class)); + linearLayout.addView(createButton(LocationActivity.class)); linearLayout.addView(createButton(ThemeStylerActivity.class)); linearLayout.addView(createButton(S3DBMapActivity.class)); linearLayout.addView(createButton(JeoIndoorMapActivity.class)); diff --git a/vtm-app/src/org/oscim/app/location/LocationHandler.java b/vtm-app/src/org/oscim/app/location/LocationHandler.java index 723e4f770..e4839a3a7 100644 --- a/vtm-app/src/org/oscim/app/location/LocationHandler.java +++ b/vtm-app/src/org/oscim/app/location/LocationHandler.java @@ -2,6 +2,7 @@ * Copyright 2010, 2011, 2012 mapsforge.org * Copyright 2013 Hannes Janetzek * Copyright 2013 Ahmad Al-saleem + * Copyright 2016 devemux86 * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free Software @@ -31,7 +32,7 @@ import org.slf4j.LoggerFactory; public class LocationHandler implements LocationListener { - final static Logger log = LoggerFactory.getLogger(LocationHandler.class); + private final static Logger log = LoggerFactory.getLogger(LocationHandler.class); public enum Mode { OFF, @@ -43,7 +44,7 @@ public enum Mode { private final static int SHOW_LOCATION_ZOOM = 14; private final LocationManager mLocationManager; - private final LocationOverlay mLocationOverlay; + private final LocationLayerImpl mLocationLayer; private Mode mMode = Mode.OFF; @@ -54,7 +55,7 @@ public LocationHandler(TileMap tileMap, Compass compass) { mLocationManager = (LocationManager) tileMap .getSystemService(Context.LOCATION_SERVICE); - mLocationOverlay = new LocationOverlay(App.map, compass); + mLocationLayer = new LocationLayerImpl(App.map, compass); mMapPosition = new MapPosition(); } @@ -115,13 +116,13 @@ private boolean enableShowMyLocation() { if (location == null) return false; - mLocationOverlay.setEnabled(true); - mLocationOverlay.setPosition(location.getLatitude(), + mLocationLayer.setEnabled(true); + mLocationLayer.setPosition(location.getLatitude(), location.getLongitude(), location.getAccuracy()); // FIXME -> implement LayerGroup - App.map.layers().add(4, mLocationOverlay); + App.map.layers().add(4, mLocationLayer); App.map.updateMap(true); return true; @@ -133,9 +134,9 @@ private boolean enableShowMyLocation() { private boolean disableShowMyLocation() { mLocationManager.removeUpdates(this); - mLocationOverlay.setEnabled(false); + mLocationLayer.setEnabled(false); - App.map.layers().remove(mLocationOverlay); + App.map.layers().remove(mLocationLayer); App.map.updateMap(true); return true; @@ -199,7 +200,7 @@ public void onLocationChanged(Location location) { App.map.setMapPosition(mMapPosition); } - mLocationOverlay.setPosition(lat, lon, location.getAccuracy()); + mLocationLayer.setPosition(lat, lon, location.getAccuracy()); } @Override diff --git a/vtm-app/src/org/oscim/app/location/LocationLayerImpl.java b/vtm-app/src/org/oscim/app/location/LocationLayerImpl.java new file mode 100644 index 000000000..3f4ad4687 --- /dev/null +++ b/vtm-app/src/org/oscim/app/location/LocationLayerImpl.java @@ -0,0 +1,38 @@ +/* + * Copyright 2013 Ahmad Saleem + * Copyright 2013 Hannes Janetzek + * Copyright 2016 devemux86 + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ +package org.oscim.app.location; + +import org.oscim.layers.LocationLayer; +import org.oscim.map.Map; + +class LocationLayerImpl extends LocationLayer { + private final Compass mCompass; + + LocationLayerImpl(Map map, Compass compass) { + super(map); + mCompass = compass; + + locationRenderer.setCallback(compass); + } + + @Override + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + + mCompass.setEnabled(enabled); + } +} diff --git a/vtm-app/src/org/oscim/app/location/LocationOverlay.java b/vtm/src/org/oscim/layers/LocationLayer.java similarity index 70% rename from vtm-app/src/org/oscim/app/location/LocationOverlay.java rename to vtm/src/org/oscim/layers/LocationLayer.java index cf63f662a..73d2185c3 100644 --- a/vtm-app/src/org/oscim/app/location/LocationOverlay.java +++ b/vtm/src/org/oscim/layers/LocationLayer.java @@ -14,31 +14,19 @@ * You should have received a copy of the GNU Lesser General Public License along with * this program. If not, see . */ -package org.oscim.app.location; +package org.oscim.layers; import org.oscim.core.MercatorProjection; -import org.oscim.layers.Layer; import org.oscim.map.Map; import org.oscim.renderer.LocationRenderer; -public class LocationOverlay extends Layer { - private final Compass mCompass; - private final LocationRenderer mLocationRenderer; +public class LocationLayer extends Layer { + public final LocationRenderer locationRenderer; - public LocationOverlay(Map map, Compass compass) { + public LocationLayer(Map map) { super(map); - mCompass = compass; - mRenderer = mLocationRenderer = new LocationRenderer(mMap, this); - mLocationRenderer.setCallback(compass); - } - - public void setPosition(double latitude, double longitude, double accuracy) { - double x = MercatorProjection.longitudeToX(longitude); - double y = MercatorProjection.latitudeToY(latitude); - double radius = accuracy / MercatorProjection.groundResolution(latitude, 1); - mLocationRenderer.setLocation(x, y, radius); - mLocationRenderer.animate(true); + mRenderer = locationRenderer = new LocationRenderer(mMap, this); } @Override @@ -49,8 +37,14 @@ public void setEnabled(boolean enabled) { super.setEnabled(enabled); if (!enabled) - mLocationRenderer.animate(false); + locationRenderer.animate(false); + } - mCompass.setEnabled(enabled); + public void setPosition(double latitude, double longitude, double accuracy) { + double x = MercatorProjection.longitudeToX(longitude); + double y = MercatorProjection.latitudeToY(latitude); + double radius = accuracy / MercatorProjection.groundResolution(latitude, 1); + locationRenderer.setLocation(x, y, radius); + locationRenderer.animate(true); } }