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 all 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 @@ -54,6 +54,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 @@ -90,6 +91,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 @@ -240,14 +256,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 @@ -284,6 +301,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
30 changes: 28 additions & 2 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 Expand Up @@ -489,9 +517,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
circleAnnotationController?.delegate = self

mapReadyResult?(nil)
print("asdasd\(channel)")
if let channel = channel {
print("asdasd2 ")
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,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 {
return MapboxGlPlatform.getInstance(_id)
.queryRenderedFeatures(point, layerIds, filter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ abstract class MapboxGlPlatform {
}

Future<List> queryRenderedFeatures(
Point<double> point, List<String> layerIds, String filter) async {
Point<double> point, List<String> layerIds, List<Object> filter) async {
throw UnimplementedError(
'queryRenderedFeatures() has not been implemented.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {

@override
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
2 changes: 1 addition & 1 deletion mapbox_gl_web/lib/src/mapbox_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class MapboxMapController extends MapboxGlPlatform

@override
Future<List> queryRenderedFeatures(
Point<double> point, List<String> layerIds, String filter) async {
Point<double> point, List<String> layerIds, List<Object> filter) async {
Map<String, dynamic> options = {};
if (layerIds.length > 0) {
options['layers'] = layerIds;
Expand Down
12 changes: 8 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,19 @@ packages:
dependency: "direct main"
description:
path: mapbox_gl_platform_interface
relative: true
source: path
ref: HEAD
resolved-ref: fa9dac81b6d0f3a5f01688a6695ee938b71874e5
url: "https://github.com/tobrun/flutter-mapbox-gl.git"
source: git
version: "0.7.0"
mapbox_gl_web:
dependency: "direct main"
description:
path: mapbox_gl_web
relative: true
source: path
ref: HEAD
resolved-ref: fa9dac81b6d0f3a5f01688a6695ee938b71874e5
url: "https://github.com/tobrun/flutter-mapbox-gl.git"
source: git
version: "0.7.0"
meta:
dependency: transitive
Expand Down