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, styleString(URL/JSON), onMapClick, queryRenderedFeatures #19

Merged
merged 3 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ android/.project
.vscode/
.idea/

android/.settings/org.eclipse.buildship.core.prefs
2 changes: 0 additions & 2 deletions android/.settings/org.eclipse.buildship.core.prefs

This file was deleted.

8 changes: 4 additions & 4 deletions android/src/main/java/com/mapbox/mapboxgl/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ static void interpretMapboxMapOptions(Object o, MapboxMapOptionsSink sink) {
if (compassEnabled != null) {
sink.setCompassEnabled(toBoolean(compassEnabled));
}
// final Object mapType = data.get("mapType");
// if (mapType != null) {
// sink.setMapType(toInt(mapType));
// }
final Object styleString = data.get("styleString");
if (styleString != null) {
sink.setStyleString(toString(styleString));
}
yoavrofe marked this conversation as resolved.
Show resolved Hide resolved
final Object minMaxZoomPreference = data.get("minMaxZoomPreference");
if (minMaxZoomPreference != null) {
final List<?> zoomPreferenceData = toList(minMaxZoomPreference);
Expand Down
13 changes: 8 additions & 5 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.Style;

import io.flutter.plugin.common.PluginRegistry;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -22,11 +23,12 @@ class MapboxMapBuilder implements MapboxMapOptionsSink {
.attributionEnabled(false);
private boolean trackCameraPosition = false;
private boolean myLocationEnabled = false;
private String styleString = Style.MAPBOX_STREETS;

MapboxMapController build(
int id, Context context, AtomicInteger state, PluginRegistry.Registrar registrar) {
final MapboxMapController controller =
new MapboxMapController(id, context, state, registrar, options);
new MapboxMapController(id, context, state, registrar, options, styleString);
Copy link
Collaborator

Choose a reason for hiding this comment

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

can style string be made part of the options class? It's a configuration similar to other options availlable. (before introducing Style.java upstream this was part of the MapboxMapOptions class).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll be glad if you could elaborate on this, an of course, if you find a better place for it I'll be happy if you can move it there.

controller.init();
controller.setMyLocationEnabled(myLocationEnabled);
controller.setTrackCameraPosition(trackCameraPosition);
Expand All @@ -49,10 +51,11 @@ public void setCameraTargetBounds(LatLngBounds bounds) {
//options.latLngBoundsForCameraTarget(bounds);
}

// @Override
// public void setMapType(int mapType) {
// options.mapType(mapType);
// }
@Override
public void setStyleString(String styleString) {
this.styleString = styleString;
//options. styleString(styleString);
}

@Override
public void setMinMaxZoomPreference(Float min, Float max) {
Expand Down
118 changes: 107 additions & 11 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.graphics.PointF;

import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
Expand All @@ -34,6 +35,16 @@
import com.mapbox.mapboxsdk.camera.CameraUpdate;

import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.geojson.Feature;
import com.mapbox.mapboxsdk.style.expressions.Expression;

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.style.layers.RasterLayer;
import com.mapbox.mapboxsdk.style.sources.RasterSource;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
Expand All @@ -42,6 +53,11 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;
import java.util.ArrayList;

import android.graphics.PointF;
import android.graphics.RectF;

/** Controller of a single MapboxMaps MapView instance. */
final class MapboxMapController
Expand All @@ -51,7 +67,7 @@ final class MapboxMapController
MapboxMap.OnCameraMoveStartedListener,
MapboxMap.OnInfoWindowClickListener,
// MapboxMap.OnMarkerClickListener,//todo: deprecated in 7
// MapboxMap.OnMapClickListener,//dddd
MapboxMap.OnMapClickListener,
MapboxMapOptionsSink,
MethodChannel.MethodCallHandler,
com.mapbox.mapboxsdk.maps.OnMapReadyCallback,
Expand All @@ -72,18 +88,22 @@ final class MapboxMapController
private MethodChannel.Result mapReadyResult;
private final int registrarActivityHashCode;
private final Context context;
private final String styleStringInitial;
LocationComponent locationComponent = null;

MapboxMapController(
int id,
Context context,
AtomicInteger activityState,
PluginRegistry.Registrar registrar,
MapboxMapOptions options) {
MapboxMapOptions options,
String styleStringInitial) {
Mapbox.getInstance(context, getAccessToken(context));
this.id = id;
this.context = context;
this.activityState = activityState;
this.registrar = registrar;
this.styleStringInitial = styleStringInitial;
this.mapView = new MapView(context, options);
// this.markers = new HashMap<>();
this.density = context.getResources().getDisplayMetrics().density;
Expand Down Expand Up @@ -192,7 +212,7 @@ private CameraPosition getCameraPosition() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.MAPBOX_STREETS);
// mapboxMap.setStyle(Style.MAPBOX_STREETS);
mapboxMap.setOnInfoWindowClickListener(this);
if (mapReadyResult != null) {
mapReadyResult.success(null);
Expand All @@ -202,7 +222,41 @@ public void onMapReady(MapboxMap mapboxMap) {
mapboxMap.addOnCameraMoveListener(this);
mapboxMap.addOnCameraIdleListener(this);
//mapboxMap.setOnMarkerClickListener(this);
updateMyLocationEnabled();
mapboxMap.addOnMapClickListener(this);
setStyleString(styleStringInitial);
// updateMyLocationEnabled();
}

@Override
public void setStyleString(String styleString) {
//check if json, url or plain string:
if (styleString==null || styleString.isEmpty()) {
Log.e(TAG,"setStyleString - string empty or null");
} else if (styleString.startsWith("{") || styleString.startsWith("[")){
mapboxMap.setStyle(new Style.Builder().fromJson(styleString), onStyleLoadedCallback);
} else {
mapboxMap.setStyle(new Style.Builder().fromUrl(styleString), onStyleLoadedCallback);
}
}

Style.OnStyleLoaded onStyleLoadedCallback = new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent();
}
};

@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent() {
if (hasLocationPermission()) {
locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(context, mapboxMap.getStyle());
locationComponent.setLocationComponentEnabled(true);
locationComponent.setCameraMode(CameraMode.TRACKING);
locationComponent.setRenderMode(RenderMode.COMPASS);
} else {
Log.e(TAG, "missing location permissions");
}
}

@Override
Expand Down Expand Up @@ -241,6 +295,37 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
result.success(null);
break;
}
case "map#queryRenderedFeatures":
{
Map<String, Object> reply = new HashMap<>();
List<Feature> features;

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);
if (call.hasArgument("x")) {
Double x = call.argument("x");
Double y = call.argument("y");
PointF pixel = new PointF(x.floatValue(), y.floatValue());
features = mapboxMap.queryRenderedFeatures(pixel, filterExpression, layerIds);
} else {
Double left = call.argument("left");
Double top = call.argument("top");
Double right = call.argument("right");
Double bottom = call.argument("bottom");
RectF rectF = new RectF(left.floatValue(), top.floatValue(), right.floatValue(), bottom.floatValue());
features = mapboxMap.queryRenderedFeatures(rectF, filterExpression, layerIds);
}
List<String> featuresJson = new ArrayList<>();
for (Feature feature : features) {
featuresJson.add(feature.toJson());
}
reply.put("features", featuresJson);
result.success(reply);
break;
}
// case "marker#add":
// {
// final MarkerBuilder markerBuilder = newMarkerBuilder();
Expand Down Expand Up @@ -313,12 +398,27 @@ public void onCameraIdle() {
// return (markerController != null && markerController.onTap());
// }

@Override
public boolean onMapClick(@NonNull LatLng point) {
PointF pointf = mapboxMap.getProjection().toScreenLocation(point);
final Map<String, Object> arguments = new HashMap<>(5);
arguments.put("x", pointf.x);
arguments.put("y", pointf.y);
arguments.put("lng", point.getLongitude());
arguments.put("lat", point.getLatitude());
methodChannel.invokeMethod("map#onMapClick", arguments);
return true;
}

@Override
public void dispose() {
if (disposed) {
return;
}
disposed = true;
if (locationComponent != null) {
locationComponent.setLocationComponentEnabled(false);
yoavrofe marked this conversation as resolved.
Show resolved Hide resolved
}
mapView.onDestroy();
registrar.activity().getApplication().unregisterActivityLifecycleCallbacks(this);
}
Expand Down Expand Up @@ -391,12 +491,6 @@ public void setCompassEnabled(boolean compassEnabled) {
mapboxMap.getUiSettings().setCompassEnabled(compassEnabled);
}

// @Override
// public void setMapType(int mapType) {
// throw new UnsupportedOperationException("setMapType");
// //mapboxMap.setMapType(mapType);
// }

@Override
public void setTrackCameraPosition(boolean trackCameraPosition) {
this.trackCameraPosition = trackCameraPosition;
Expand Down Expand Up @@ -445,7 +539,9 @@ public void setMyLocationEnabled(boolean myLocationEnabled) {
}

private void updateMyLocationEnabled() {
//throw new UnsupportedOperationException("updateMyLocationEnabled")
// if (locationComponent != null) {
// locationComponent.setLocationComponentEnabled(this.myLocationEnabled);
// }
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code is not used and now that we have enableLocationComponent, can we remove it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We may need some code here in the future, if we start with myLocationEnabled=false and turn it to true later.

}

private boolean hasLocationPermission() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface MapboxMapOptionsSink {

void setCompassEnabled(boolean compassEnabled);

// void setMapType(int mapType);
// TODO: styleString is not actually a part of options. consider moving
void setStyleString(String styleString);
Copy link
Collaborator

Choose a reason for hiding this comment

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

as indicated in comment, options would be a better place for this configuration.


void setMinMaxZoomPreference(Float min, Float max);

Expand Down
21 changes: 13 additions & 8 deletions example/lib/map_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MapUiBodyState extends State<MapUiBody> {
bool _compassEnabled = true;
CameraTargetBounds _cameraTargetBounds = CameraTargetBounds.unbounded;
MinMaxZoomPreference _minMaxZoomPreference = MinMaxZoomPreference.unbounded;
MapType _mapType = MapType.normal;
String _styleString = MapboxStyles.MAPBOX_STREETS;
bool _rotateGesturesEnabled = true;
bool _scrollGesturesEnabled = true;
bool _tiltGesturesEnabled = true;
Expand Down Expand Up @@ -114,14 +114,12 @@ class MapUiBodyState extends State<MapUiBody> {
);
}

Widget _mapTypeCycler() {
final MapType nextType =
MapType.values[(_mapType.index + 1) % MapType.values.length];
Widget _setStyleToSatellite() {
return FlatButton(
child: Text('change map type to $nextType'),
child: Text('change map style to Satellite'),
onPressed: () {
setState(() {
_mapType = nextType;
_styleString = MapboxStyles.SATELLITE;
});
},
);
Expand Down Expand Up @@ -191,12 +189,19 @@ class MapUiBodyState extends State<MapUiBody> {
compassEnabled: _compassEnabled,
cameraTargetBounds: _cameraTargetBounds,
minMaxZoomPreference: _minMaxZoomPreference,
mapType: _mapType,
styleString: _styleString,
rotateGesturesEnabled: _rotateGesturesEnabled,
scrollGesturesEnabled: _scrollGesturesEnabled,
tiltGesturesEnabled: _tiltGesturesEnabled,
zoomGesturesEnabled: _zoomGesturesEnabled,
myLocationEnabled: _myLocationEnabled,
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]);
}
}
);

final List<Widget> columnChildren = <Widget>[
Expand Down Expand Up @@ -226,7 +231,7 @@ class MapUiBodyState extends State<MapUiBody> {
Text(_isMoving ? '(Camera moving)' : '(Camera idle)'),
_compassToggler(),
_latLngBoundsToggler(),
_mapTypeCycler(),
_setStyleToSatellite(),
_zoomBoundsToggler(),
_rotateToggler(),
_scrollToggler(),
Expand Down
1 change: 1 addition & 0 deletions lib/mapbox_gl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
library mapbox_gl;

import 'dart:async';
import 'dart:math';
import 'dart:ui';

import 'package:flutter/foundation.dart';
Expand Down
Loading