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

Implementation of feature querying on iOS #177

Merged
merged 16 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
13 changes: 10 additions & 3 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineCallback;
import com.mapbox.android.core.location.LocationEngineProvider;
Expand Down Expand Up @@ -495,9 +498,13 @@ public void onCancel() {

String[] layerIds = ((List<String>) call.argument("layerIds")).toArray(new String[0]);

String filter = (String) call.argument("filter");

Expression filterExpression = filter == null ? null : new Expression(filter);
List<Object> filter = call.argument("filter");
JsonElement jsonElement = filter == null ? null : new Gson().toJsonTree(filter);
JsonArray jsonArray = null;
if (jsonElement != null && jsonElement.isJsonArray()) {
jsonArray = jsonElement.getAsJsonArray();
}
Expression filterExpression = jsonArray == null ? null : Expression.Converter.convert(jsonArray);
if (call.hasArgument("x")) {
Double x = call.argument("x");
Double y = call.argument("y");
Expand Down
22 changes: 20 additions & 2 deletions example/lib/map_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class MapUiBodyState extends State<MapUiBody> {
bool _myLocationEnabled = true;
bool _telemetryEnabled = true;
MyLocationTrackingMode _myLocationTrackingMode = MyLocationTrackingMode.Tracking;
List<Object> _featureQueryFilter;

@override
void initState() {
Expand Down Expand Up @@ -86,6 +87,21 @@ class MapUiBodyState extends State<MapUiBody> {
);
}

Widget _queryFilterToggler() {
return FlatButton(
child: Text('filter zoo on click ${ _featureQueryFilter == null ? 'disabled' : 'enabled'}'),
onPressed: () {
setState(() {
if (_featureQueryFilter == null) {
_featureQueryFilter = ["==", ["get", "type"] , "zoo"];
} else {
_featureQueryFilter = null;
}
});
},
);
}

Widget _compassToggler() {
return FlatButton(
child: Text('${_compassEnabled ? 'disable' : 'enable'} compasss'),
Expand Down Expand Up @@ -236,14 +252,15 @@ class MapUiBodyState extends State<MapUiBody> {
myLocationRenderMode: MyLocationRenderMode.GPS,
onMapClick: (point, latLng) async {
print("Map click: ${point.x},${point.y} ${latLng.latitude}/${latLng.longitude}");
List features = await mapController.queryRenderedFeatures(point, [],null);
print("Filter $_featureQueryFilter");
List features = await mapController.queryRenderedFeatures(point, [], _featureQueryFilter);
if (features.length>0) {
print(features[0]);
}
},
onMapLongClick: (point, latLng) async {
print("Map long press: ${point.x},${point.y} ${latLng.latitude}/${latLng.longitude}");
List features = await mapController.queryRenderedFeatures(point, [],null);
List features = await mapController.queryRenderedFeatures(point, [], null);
if (features.length>0) {
print(features[0]);
}
Expand Down Expand Up @@ -280,6 +297,7 @@ class MapUiBodyState extends State<MapUiBody> {
Text('camera zoom: ${_position.zoom}'),
Text('camera tilt: ${_position.tilt}'),
Text(_isMoving ? '(Camera moving)' : '(Camera idle)'),
_queryFilterToggler(),
_compassToggler(),
_myLocationTrackingModeCycler(),
_latLngBoundsToggler(),
Expand Down
28 changes: 28 additions & 0 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
style.localizeLabels(into: locale)
}
result(nil)
case "map#queryRenderedFeatures":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
let layerIds = arguments["layerIds"] as? Set<String>
var filterExpression: NSPredicate?
if let filter = arguments["filter"] as? [Any] {
filterExpression = NSPredicate(mglJSONObject: filter)
}
var reply = [String: NSObject]()
var features:[MGLFeature] = []
if let x = arguments["x"] as? Double, let y = arguments["y"] as? Double {
features = mapView.visibleFeatures(at: CGPoint(x: x, y: y), styleLayerIdentifiers: layerIds, predicate: filterExpression)
}
if let top = arguments["top"] as? Double,
let bottom = arguments["bottom"] as? Double,
let left = arguments["left"] as? Double,
let right = arguments["right"] as? Double {
features = mapView.visibleFeatures(in: CGRect(x: left, y: top, width: right, height: bottom), styleLayerIdentifiers: layerIds, predicate: filterExpression)
}
var featuresJson = [String]()
for feature in features {
let dictionary = feature.geoJSONDictionary()
if let theJSONData = try? JSONSerialization.data(withJSONObject: dictionary, options: []),
let theJSONText = String(data: theJSONData, encoding: .ascii) {
featuresJson.append(theJSONText)
}
}
reply["features"] = featuresJson as NSObject
result(reply)
case "map#setTelemetryEnabled":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
let telemetryEnabled = arguments["enabled"] as? Bool
Expand Down
2 changes: 1 addition & 1 deletion lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ class MapboxMapController extends ChangeNotifier {
}

Future<List> queryRenderedFeatures(
Point<double> point, List<String> layerIds, String filter) async {
Point<double> point, List<String> layerIds, List<Object> filter) async {
try {
final Map<Object, Object> reply = await _channel.invokeMethod(
'map#queryRenderedFeatures',
Expand Down
9 changes: 5 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
version: "1.14.12"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -19,7 +19,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.1.8"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -40,4 +40,5 @@ packages:
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.0.0-dev.68.0 <3.0.0"
dart: ">=2.2.2 <3.0.0"
flutter: ">=1.10.0 <2.0.0"