mirror of https://github.com/tasks/tasks
Replace Mapbox maps with OsmDroid
parent
f168107a31
commit
b0777101e8
@ -1,133 +0,0 @@
|
|||||||
package org.tasks.location;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.Context;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import com.mapbox.mapboxsdk.Mapbox;
|
|
||||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
|
||||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
|
||||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
|
||||||
import com.mapbox.mapboxsdk.camera.CameraUpdate;
|
|
||||||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
|
|
||||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
|
||||||
import com.mapbox.mapboxsdk.location.LocationComponent;
|
|
||||||
import com.mapbox.mapboxsdk.location.modes.CameraMode;
|
|
||||||
import com.mapbox.mapboxsdk.location.modes.RenderMode;
|
|
||||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
|
||||||
import com.mapbox.mapboxsdk.maps.MapboxMap.OnMarkerClickListener;
|
|
||||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
|
||||||
import com.mapbox.mapboxsdk.maps.Style;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import org.tasks.R;
|
|
||||||
import org.tasks.data.Place;
|
|
||||||
|
|
||||||
public class MapboxMapFragment implements MapFragment, OnMapReadyCallback, OnMarkerClickListener {
|
|
||||||
|
|
||||||
private static final String FRAG_TAG_MAP = "frag_tag_map";
|
|
||||||
private final Context context;
|
|
||||||
private MapFragmentCallback callbacks;
|
|
||||||
private boolean dark;
|
|
||||||
private MapboxMap map;
|
|
||||||
private final Map<Marker, Place> markers = new HashMap<>();
|
|
||||||
|
|
||||||
public MapboxMapFragment(Context context) {
|
|
||||||
this.context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(FragmentManager fragmentManager, MapFragmentCallback callbacks, boolean dark) {
|
|
||||||
this.callbacks = callbacks;
|
|
||||||
this.dark = dark;
|
|
||||||
Mapbox.getInstance(context, context.getString(R.string.mapbox_key));
|
|
||||||
com.mapbox.mapboxsdk.maps.SupportMapFragment mapFragment =
|
|
||||||
(com.mapbox.mapboxsdk.maps.SupportMapFragment)
|
|
||||||
fragmentManager.findFragmentByTag(FRAG_TAG_MAP);
|
|
||||||
if (mapFragment == null) {
|
|
||||||
mapFragment = new com.mapbox.mapboxsdk.maps.SupportMapFragment();
|
|
||||||
fragmentManager.beginTransaction().replace(R.id.map, mapFragment).commit();
|
|
||||||
}
|
|
||||||
mapFragment.getMapAsync(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public MapPosition getMapPosition() {
|
|
||||||
if (map == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
CameraPosition cameraPosition = map.getCameraPosition();
|
|
||||||
LatLng target = cameraPosition.target;
|
|
||||||
return new MapPosition(
|
|
||||||
target.getLatitude(), target.getLongitude(), (float) cameraPosition.zoom);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void movePosition(MapPosition mapPosition, boolean animate) {
|
|
||||||
CameraUpdate cameraUpdate =
|
|
||||||
CameraUpdateFactory.newCameraPosition(
|
|
||||||
new CameraPosition.Builder()
|
|
||||||
.target(new LatLng(mapPosition.getLatitude(), mapPosition.getLongitude()))
|
|
||||||
.zoom(mapPosition.getZoom())
|
|
||||||
.build());
|
|
||||||
if (animate) {
|
|
||||||
map.animateCamera(cameraUpdate);
|
|
||||||
} else {
|
|
||||||
map.moveCamera(cameraUpdate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setMarkers(List<Place> places) {
|
|
||||||
if (map == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (Marker marker : map.getMarkers()) {
|
|
||||||
map.removeMarker(marker);
|
|
||||||
}
|
|
||||||
markers.clear();
|
|
||||||
for (Place place : places) {
|
|
||||||
Marker marker =
|
|
||||||
map.addMarker(
|
|
||||||
new MarkerOptions()
|
|
||||||
.setPosition(new LatLng(place.getLatitude(), place.getLongitude())));
|
|
||||||
markers.put(marker, place);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void disableGestures() {
|
|
||||||
map.getUiSettings().setAllGesturesEnabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
|
||||||
@Override
|
|
||||||
public void showMyLocation() {
|
|
||||||
LocationComponent locationComponent = map.getLocationComponent();
|
|
||||||
locationComponent.activateLocationComponent(context, map.getStyle());
|
|
||||||
locationComponent.setLocationComponentEnabled(true);
|
|
||||||
locationComponent.setCameraMode(CameraMode.NONE);
|
|
||||||
locationComponent.setRenderMode(RenderMode.NORMAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMapReady(@NonNull MapboxMap mapboxMap) {
|
|
||||||
map = mapboxMap;
|
|
||||||
map.getUiSettings().setRotateGesturesEnabled(false);
|
|
||||||
map.setOnMarkerClickListener(this);
|
|
||||||
map.setStyle(dark ? Style.DARK : Style.MAPBOX_STREETS, style -> callbacks.onMapReady(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getMarkerId() {
|
|
||||||
return R.id.mapbox_marker;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onMarkerClick(@NonNull Marker marker) {
|
|
||||||
Place place = markers.get(marker);
|
|
||||||
callbacks.onPlaceSelected(place);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,98 @@
|
|||||||
|
package org.tasks.location
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import org.osmdroid.config.Configuration
|
||||||
|
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
|
||||||
|
import org.osmdroid.util.GeoPoint
|
||||||
|
import org.osmdroid.views.CustomZoomButtonsController
|
||||||
|
import org.osmdroid.views.MapView
|
||||||
|
import org.osmdroid.views.overlay.Marker
|
||||||
|
import org.osmdroid.views.overlay.TilesOverlay
|
||||||
|
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
|
||||||
|
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.data.Place
|
||||||
|
import org.tasks.location.MapFragment.MapFragmentCallback
|
||||||
|
|
||||||
|
class OsmMapFragment(private val context: Context) : MapFragment {
|
||||||
|
private lateinit var callbacks: MapFragmentCallback
|
||||||
|
private lateinit var map: MapView
|
||||||
|
private var locationOverlay: MyLocationNewOverlay? = null
|
||||||
|
|
||||||
|
override fun init(activity: AppCompatActivity, callbacks: MapFragmentCallback, dark: Boolean) {
|
||||||
|
this.callbacks = callbacks
|
||||||
|
Configuration.getInstance()
|
||||||
|
.load(activity, PreferenceManager.getDefaultSharedPreferences(activity))
|
||||||
|
map = MapView(activity).apply {
|
||||||
|
isTilesScaledToDpi = true
|
||||||
|
setTileSource(TileSourceFactory.MAPNIK)
|
||||||
|
zoomController.setVisibility(CustomZoomButtonsController.Visibility.NEVER)
|
||||||
|
setMultiTouchControls(true)
|
||||||
|
if (dark) {
|
||||||
|
overlayManager.tilesOverlay.setColorFilter(TilesOverlay.INVERT_COLORS)
|
||||||
|
}
|
||||||
|
activity.findViewById<ViewGroup>(R.id.map).addView(this)
|
||||||
|
}
|
||||||
|
callbacks.onMapReady(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getMapPosition(): MapPosition {
|
||||||
|
val center = map.mapCenter
|
||||||
|
return MapPosition(center.latitude, center.longitude, map.zoomLevelDouble.toFloat())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun movePosition(mapPosition: MapPosition, animate: Boolean) {
|
||||||
|
val controller = map.controller
|
||||||
|
controller.setZoom(mapPosition.zoom.toDouble())
|
||||||
|
val geoPoint = GeoPoint(mapPosition.latitude, mapPosition.longitude)
|
||||||
|
if (animate) {
|
||||||
|
controller.animateTo(geoPoint)
|
||||||
|
} else {
|
||||||
|
controller.setCenter(geoPoint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setMarkers(places: List<Place>) {
|
||||||
|
val overlays = map.overlays
|
||||||
|
overlays.removeIf { it is Marker }
|
||||||
|
for (place in places) {
|
||||||
|
overlays.add(Marker(map).apply {
|
||||||
|
position = GeoPoint(place.latitude, place.longitude)
|
||||||
|
setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER)
|
||||||
|
icon = ContextCompat.getDrawable(context, R.drawable.map_marker_padding)!!.mutate()
|
||||||
|
setOnMarkerClickListener { _, _ ->
|
||||||
|
callbacks.onPlaceSelected(place)
|
||||||
|
false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun disableGestures() = map.setMultiTouchControls(false)
|
||||||
|
|
||||||
|
@SuppressLint("MissingPermission")
|
||||||
|
override fun showMyLocation() {
|
||||||
|
locationOverlay = MyLocationNewOverlay(GpsMyLocationProvider(context), map).apply {
|
||||||
|
enableMyLocation()
|
||||||
|
enableAutoStop = false
|
||||||
|
}
|
||||||
|
map.overlays.add(locationOverlay)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
locationOverlay?.disableMyLocation()
|
||||||
|
map.onPause()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
locationOverlay?.enableMyLocation()
|
||||||
|
map.onResume()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() = map.onDetach()
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item
|
||||||
|
android:bottom="48dp"
|
||||||
|
android:left="0dp"
|
||||||
|
android:right="0dp"
|
||||||
|
android:top="0dp">
|
||||||
|
|
||||||
|
<selector>
|
||||||
|
<item android:drawable="@drawable/ic_map_marker_select_red_48dp"/>
|
||||||
|
</selector>
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
@ -1,2 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<shape />
|
|
Loading…
Reference in New Issue