Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add location tracking mode #21

Merged
merged 4 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ static void interpretMapboxMapOptions(Object o, MapboxMapOptionsSink sink) {
if (myLocationEnabled != null) {
sink.setMyLocationEnabled(toBoolean(myLocationEnabled));
}
final Object myLocationTrackingMode = data.get("myLocationTrackingMode");
if (myLocationTrackingMode != null) {
sink.setMyLocationTrackingMode(toInt(myLocationTrackingMode));
}
final Object myLocationVerticalAlignment = data.get("myLocationVerticalAlignment");
yoavrofe marked this conversation as resolved.
Show resolved Hide resolved
if (myLocationVerticalAlignment != null) {
sink.setMyLocationVerticalAlignment(toInt(myLocationVerticalAlignment));
}
}

// static void interpretMarkerOptions(Object o, MarkerOptionsSink sink) {
Expand Down
14 changes: 14 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class MapboxMapBuilder implements MapboxMapOptionsSink {
.attributionEnabled(false);
private boolean trackCameraPosition = false;
private boolean myLocationEnabled = false;
private int myLocationTrackingMode = 0;
private int myLocationVerticalAlignment = 0;
private String styleString = Style.MAPBOX_STREETS;

MapboxMapController build(
Expand All @@ -31,6 +33,8 @@ MapboxMapController build(
new MapboxMapController(id, context, state, registrar, options, styleString);
controller.init();
controller.setMyLocationEnabled(myLocationEnabled);
controller.setMyLocationTrackingMode(myLocationTrackingMode);
controller.setMyLocationTrackingMode(myLocationTrackingMode);
yoavrofe marked this conversation as resolved.
Show resolved Hide resolved
controller.setTrackCameraPosition(trackCameraPosition);
return controller;
}
Expand Down Expand Up @@ -96,4 +100,14 @@ public void setZoomGesturesEnabled(boolean zoomGesturesEnabled) {
public void setMyLocationEnabled(boolean myLocationEnabled) {
this.myLocationEnabled = myLocationEnabled;
}

@Override
public void setMyLocationTrackingMode(int myLocationTrackingMode) {
this.myLocationTrackingMode = myLocationTrackingMode;
}

@Override
public void setMyLocationVerticalAlignment(int myLocationVerticalAlignment) {
this.myLocationVerticalAlignment = myLocationVerticalAlignment;
}
}
65 changes: 60 additions & 5 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.location.LocationComponentOptions;
import com.mapbox.mapboxsdk.location.OnCameraTrackingChangedListener;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
Expand Down Expand Up @@ -71,6 +73,7 @@ final class MapboxMapController
MapboxMapOptionsSink,
MethodChannel.MethodCallHandler,
com.mapbox.mapboxsdk.maps.OnMapReadyCallback,
OnCameraTrackingChangedListener,
//OnMarkerTappedListener,
PlatformView {
private static final String TAG = "MapboxMapController";
Expand All @@ -83,6 +86,8 @@ final class MapboxMapController
private MapboxMap mapboxMap;
private boolean trackCameraPosition = false;
private boolean myLocationEnabled = false;
private int myLocationTrackingMode = 0;
private int myLocationVerticalAlignment = 0;
private boolean disposed = false;
private final float density;
private MethodChannel.Result mapReadyResult;
Expand All @@ -91,6 +96,10 @@ final class MapboxMapController
private final String styleStringInitial;
LocationComponent locationComponent = null;

private static final int LocationVerticalAlignmentCenter = 0; //TODO: implement
private static final int LocationVerticalAlignmentTop = 1; //TODO: implement
private static final int LocationVerticalAlignmentBottom = 2; //TODO: implement
yoavrofe marked this conversation as resolved.
Show resolved Hide resolved

MapboxMapController(
int id,
Context context,
Expand Down Expand Up @@ -249,11 +258,16 @@ public void onStyleLoaded(@NonNull Style style) {
@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent() {
if (hasLocationPermission()) {
LocationComponentOptions locationComponentOptions = LocationComponentOptions.builder(context)
.trackingGesturesManagement(true)
.build();
locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(context, mapboxMap.getStyle());
locationComponent.activateLocationComponent(context, mapboxMap.getStyle(), locationComponentOptions);
locationComponent.setLocationComponentEnabled(true);
locationComponent.setCameraMode(CameraMode.TRACKING);
locationComponent.setRenderMode(RenderMode.COMPASS);
updateMyLocationTrackingMode();
setMyLocationTrackingMode(this.myLocationTrackingMode);
locationComponent.addOnCameraTrackingChangedListener(this);
} else {
Log.e(TAG, "missing location permissions");
}
Expand Down Expand Up @@ -385,6 +399,15 @@ public void onCameraIdle() {
methodChannel.invokeMethod("camera#onIdle", Collections.singletonMap("map", id));
}

@Override
public void onCameraTrackingChanged(int currentMode) {
}

@Override
public void onCameraTrackingDismissed() {
methodChannel.invokeMethod("map#onCameraTrackingDismissed",new HashMap<>());
}

// @Override
// public void onMarkerTapped(Marker marker) {
// final Map<String, Object> arguments = new HashMap<>(2);
Expand Down Expand Up @@ -538,10 +561,42 @@ public void setMyLocationEnabled(boolean myLocationEnabled) {
}
}

@Override
public void setMyLocationTrackingMode(int myLocationTrackingMode) {
if (this.myLocationTrackingMode == myLocationTrackingMode) {
return;
}
this.myLocationTrackingMode = myLocationTrackingMode;
if (mapboxMap != null && locationComponent != null) {
updateMyLocationTrackingMode();
}
}

@Override
public void setMyLocationVerticalAlignment(int myLocationVerticalAlignment) {
if (this.myLocationVerticalAlignment == myLocationVerticalAlignment) {
return;
}
this.myLocationVerticalAlignment = myLocationVerticalAlignment;
if (mapboxMap != null && locationComponent != null) {
updateMyLocationVerticalAlignment();
}
}

private void updateMyLocationEnabled() {
// if (locationComponent != null) {
// locationComponent.setLocationComponentEnabled(this.myLocationEnabled);
// }
//TODO: call location initialization if changed to true and not initialized yet.;
//Show/Hide use location as needed
}

private void updateMyLocationTrackingMode() {
int[] mapboxTrackingModes = new int[]{ CameraMode.NONE, CameraMode.TRACKING, CameraMode.TRACKING_COMPASS, CameraMode.TRACKING_GPS };
locationComponent.setCameraMode(mapboxTrackingModes[this.myLocationTrackingMode]);
}

private void updateMyLocationVerticalAlignment() {
//not implemented yet.
// Learn from here:
// https://github.com/mapbox/react-native-mapbox-gl/blob/master/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java#L1411
yoavrofe marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean hasLocationPermission() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ interface MapboxMapOptionsSink {
void setZoomGesturesEnabled(boolean zoomGesturesEnabled);

void setMyLocationEnabled(boolean myLocationEnabled);

void setMyLocationTrackingMode(int myLocationTrackingMode);

void setMyLocationVerticalAlignment(int myLocationVerticalAlignment);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not actionable atm but we might need to look into either using enums or using @StringDef/@IntDef for type safety

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's on the Java side, I thought it's enough to rely on this array.

}
4 changes: 0 additions & 4 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */ = {isa = PBXBuildFile; fileRef = 2D5378251FAA1A9400D5DBA9 /* flutter_assets */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -40,7 +39,6 @@
/* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
2D5378251FAA1A9400D5DBA9 /* flutter_assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = flutter_assets; path = Flutter/flutter_assets; sourceTree = SOURCE_ROOT; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -73,7 +71,6 @@
9740EEB11CF90186004384FC /* Flutter */ = {
isa = PBXGroup;
children = (
2D5378251FAA1A9400D5DBA9 /* flutter_assets */,
3B80C3931E831B6300D905FE /* App.framework */,
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */,
9740EEBA1CF902C7004384FC /* Flutter.framework */,
Expand Down Expand Up @@ -190,7 +187,6 @@
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
9740EEB41CF90195004384FC /* Debug.xcconfig in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */,
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
yoavrofe marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>
yoavrofe marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 21 additions & 0 deletions example/lib/map_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class MapUiBodyState extends State<MapUiBody> {
bool _tiltGesturesEnabled = true;
bool _zoomGesturesEnabled = true;
bool _myLocationEnabled = true;
MyLocationTrackingMode _myLocationTrackingMode = MyLocationTrackingMode.Tracking;

@override
void initState() {
Expand All @@ -71,6 +72,19 @@ class MapUiBodyState extends State<MapUiBody> {
super.dispose();
}

Widget _myLocationTrackingModeCycler() {
final MyLocationTrackingMode nextType =
MyLocationTrackingMode.values[(_myLocationTrackingMode.index + 1) % MyLocationTrackingMode.values.length];
return FlatButton(
child: Text('change to $nextType'),
onPressed: () {
setState(() {
_myLocationTrackingMode = nextType;
});
},
);
}

Widget _compassToggler() {
return FlatButton(
child: Text('${_compassEnabled ? 'disable' : 'enable'} compasss'),
Expand Down Expand Up @@ -195,12 +209,18 @@ class MapUiBodyState extends State<MapUiBody> {
tiltGesturesEnabled: _tiltGesturesEnabled,
zoomGesturesEnabled: _zoomGesturesEnabled,
myLocationEnabled: _myLocationEnabled,
myLocationTrackingMode: _myLocationTrackingMode,
onMapClick: (point, latLng) async {
print("${point.x},${point.y} ${latLng.latitude}/${latLng.longitude}");
List features = await mapController.queryRenderedFeatures(point, [],null);
if (features.length>0) {
print(features[0]);
}
},
onCameraTrackingDismissed: () {
this.setState(() {
_myLocationTrackingMode = MyLocationTrackingMode.None;
});
}
);

Expand Down Expand Up @@ -230,6 +250,7 @@ class MapUiBodyState extends State<MapUiBody> {
Text('camera tilt: ${_position.tilt}'),
Text(_isMoving ? '(Camera moving)' : '(Camera idle)'),
_compassToggler(),
_myLocationTrackingModeCycler(),
_latLngBoundsToggler(),
_setStyleToSatellite(),
_zoomBoundsToggler(),
Expand Down
58 changes: 36 additions & 22 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ part of mapbox_gl;

typedef void OnMapClickCallback(Point<double> point, LatLng coordinates);

typedef void OnCameraTrackingDismissedCallback();

/// Controller for a single MapboxMap instance running on the host platform.
///
/// Change listeners are notified upon changes to any of
Expand All @@ -20,7 +22,8 @@ typedef void OnMapClickCallback(Point<double> point, LatLng coordinates);
/// Marker tap events can be received by adding callbacks to [onMarkerTapped].
class MapboxMapController extends ChangeNotifier {
MapboxMapController._(
this._id, MethodChannel channel, CameraPosition initialCameraPosition, {this.onMapClick})
this._id, MethodChannel channel, CameraPosition initialCameraPosition,
{this.onMapClick, this.onCameraTrackingDismissed})
: assert(_id != null),
assert(channel != null),
_channel = channel {
Expand All @@ -29,18 +32,24 @@ class MapboxMapController extends ChangeNotifier {
}

static Future<MapboxMapController> init(
int id, CameraPosition initialCameraPosition, {OnMapClickCallback onMapClick}) async {
int id, CameraPosition initialCameraPosition,
{OnMapClickCallback onMapClick,
OnCameraTrackingDismissedCallback onCameraTrackingDismissed}) async {
assert(id != null);
final MethodChannel channel =
MethodChannel('plugins.flutter.io/mapbox_maps_$id');
await channel.invokeMethod('map#waitForMap');
return MapboxMapController._(id, channel, initialCameraPosition, onMapClick: onMapClick);
return MapboxMapController._(id, channel, initialCameraPosition,
onMapClick: onMapClick,
onCameraTrackingDismissed: onCameraTrackingDismissed);
}

final MethodChannel _channel;

final OnMapClickCallback onMapClick;

final OnCameraTrackingDismissedCallback onCameraTrackingDismissed;

/// Callbacks to receive tap events for markers placed on this map.
final ArgumentCallbacks<Marker> onMarkerTapped = ArgumentCallbacks<Marker>();

Expand Down Expand Up @@ -103,6 +112,11 @@ class MapboxMapController extends ChangeNotifier {
onMapClick(Point<double>(x, y), LatLng(lat, lng));
}
break;
case 'map#onCameraTrackingDismissed':
if (onCameraTrackingDismissed != null) {
onCameraTrackingDismissed();
}
break;
default:
throw MissingPluginException();
}
Expand Down Expand Up @@ -246,23 +260,23 @@ class MapboxMapController extends ChangeNotifier {
}
}

Future<List> queryRenderedFeaturesInRect(Rect rect, List<String> layerIds, String filter) async {
try {
final Map<Object, Object> reply = await _channel.invokeMethod(
'map#queryRenderedFeatures',
<String, Object>{
'left': rect.left,
'top': rect.top,
'right': rect.right,
'bottom': rect.bottom,
'layerIds': layerIds,
'filter': filter,
},
);
return reply['features'];
} on PlatformException catch (e) {
return new Future.error(e);
}
}

Future<List> queryRenderedFeaturesInRect(
Rect rect, List<String> layerIds, String filter) async {
try {
final Map<Object, Object> reply = await _channel.invokeMethod(
'map#queryRenderedFeatures',
<String, Object>{
'left': rect.left,
'top': rect.top,
'right': rect.right,
'bottom': rect.bottom,
'layerIds': layerIds,
'filter': filter,
},
);
return reply['features'];
} on PlatformException catch (e) {
return new Future.error(e);
}
}
}
Loading