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

Dynamically set properties on runtime using setProperties #1333

Closed
wants to merge 14 commits into from
Closed
71 changes: 71 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.offline.OfflineManager;
import com.mapbox.mapboxsdk.plugins.annotation.Circle;
import com.mapbox.mapboxsdk.plugins.localization.LocalizationPlugin;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.storage.FileSource;
Expand Down Expand Up @@ -138,6 +139,10 @@ final class MapboxMapController
private LatLng dragOrigin;
private LatLng dragPrevious;
private LatLngBounds bounds = null;
private HashMap<String, LineLayer> lineLayers = new HashMap<String, LineLayer>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a need to do this? Can’t you just the get the layer from the style?

private HashMap<String, SymbolLayer> symbolLayers = new HashMap<String, SymbolLayer>();
private HashMap<String, CircleLayer> circleLayers = new HashMap<String, CircleLayer>();

Style.OnStyleLoaded onStyleLoadedCallback =
new Style.OnStyleLoaded() {
@Override
Expand Down Expand Up @@ -422,6 +427,29 @@ private void addSymbolLayer(
if (enableInteraction) {
interactiveFeatureLayerIds.add(layerName);
}

symbolLayers.put(layerName, symbolLayer);
}

private void setLineLayerProperties(String layerName, PropertyValue[] properties){
LineLayer layer = lineLayers.get(layerName);
if(layer != null) {
layer.setProperties(properties);
}
}

private void setSymbolLayerProperties(String layerName, PropertyValue[] properties){
SymbolLayer layer = symbolLayers.get(layerName);
if (layer != null) {
layer.setProperties(properties);
}
}

private void setCircleLayerProperties(String layerName, PropertyValue[] properties) {
CircleLayer layer = circleLayers.get(layerName);
if(layer != null) {
layer.setProperties(properties);
}
}

private void addLineLayer(
Expand Down Expand Up @@ -456,6 +484,8 @@ private void addLineLayer(
if (enableInteraction) {
interactiveFeatureLayerIds.add(layerName);
}

lineLayers.put(layerName, lineLayer);
}

private void addFillLayer(
Expand Down Expand Up @@ -558,6 +588,8 @@ private void addCircleLayer(
if (enableInteraction) {
interactiveFeatureLayerIds.add(layerName);
}

circleLayers.put(layerName, circleLayer);
}

private Expression parseFilter(String filter) {
Expand Down Expand Up @@ -1429,6 +1461,42 @@ public void onError(String error) {
});
break;
}
case "lineLayer#setProperties":
{

final PropertyValue[] properties = LayerPropertyConverter.interpretLineLayerProperties(call.argument("properties"));
final String layerId = call.argument("layerId");

setLineLayerProperties(layerId, properties);

result.success(null);

break;
}
case "symbolLayer#setProperties":
{

final PropertyValue[] properties = LayerPropertyConverter.interpretSymbolLayerProperties(call.argument("properties"));
final String layerId = call.argument("layerId");

setSymbolLayerProperties(layerId, properties);

result.success(null);

break;
}
case "circleLayer#setProperties":
{

final PropertyValue[] properties = LayerPropertyConverter.interpretSymbolLayerProperties(call.argument("properties"));
final String layerId = call.argument("layerId");

setCircleLayerProperties(layerId, properties);

result.success(null);

break;
}
default:
result.notImplemented();
}
Expand Down Expand Up @@ -1989,6 +2057,9 @@ void removeLayer(String layerId) {
if (style != null && layerId != null) {
style.removeLayer(layerId);
interactiveFeatureLayerIds.remove(layerId);
lineLayers.remove(layerId);
symbolLayers.remove(layerId);
circleLayers.remove(layerId);
}
}

Expand Down
11 changes: 11 additions & 0 deletions example/android/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1685582061856</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
11 changes: 11 additions & 0 deletions example/android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
arguments=--init-script /var/folders/x9/_754gdq53b1dqkmjk09_4q540000gn/T/d146c9752a26f79b52047fb6dc6ed385d064e120494f96f08ca63a317c41f94c.gradle --init-script /var/folders/x9/_754gdq53b1dqkmjk09_4q540000gn/T/52cde0cfcf3e28b8b7510e992210d9614505e0911af0c190bd590d7158574963.gradle
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.4.2))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/Applications/Android Studio.app/Contents/jbr/Contents/Home
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
platform :ios, '9.0'
platform :ios, '11.0'
use_frameworks!

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
Expand Down
47 changes: 37 additions & 10 deletions ios/Classes/Convert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Convert {
}
}

class func parseCameraUpdate(cameraUpdate: [Any], mapView: MGLMapView) -> MGLMapCamera? {
class func parseCameraUpdate(cameraUpdate: [Any], mapView: MGLMapView, completionHandler: (() -> Void)?, duration: TimeInterval?) -> MGLMapCamera? {
guard let type = cameraUpdate[0] as? String else { return nil }
switch type {
case "newCameraPosition":
Expand All @@ -89,15 +89,42 @@ class Convert {
guard let paddingTop = cameraUpdate[3] as? CGFloat else { return nil }
guard let paddingRight = cameraUpdate[4] as? CGFloat else { return nil }
guard let paddingBottom = cameraUpdate[5] as? CGFloat else { return nil }
return mapView.cameraThatFitsCoordinateBounds(
MGLCoordinateBounds.fromArray(bounds),
edgePadding: UIEdgeInsets(
top: paddingTop,
left: paddingLeft,
bottom: paddingBottom,
right: paddingRight
)
)

let newBounds: MGLCoordinateBounds = MGLCoordinateBounds.fromArray(bounds)
Copy link
Collaborator

Choose a reason for hiding this comment

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

whats the intent of this change? why only on ios?


let coordinates: [CLLocationCoordinate2D] = [
CLLocationCoordinate2D(latitude: newBounds.ne.latitude, longitude: newBounds.sw.longitude),
newBounds.sw,
CLLocationCoordinate2D(latitude: newBounds.sw.latitude, longitude: newBounds.ne.longitude),
newBounds.ne
]

let padding : UIEdgeInsets = UIEdgeInsets(
top: paddingTop,
left: paddingLeft,
bottom: paddingBottom,
right: paddingRight
)


if let animDur = duration {
mapView.setVisibleCoordinates(coordinates,
count: UInt(coordinates.count), edgePadding: padding, direction: mapView.direction,
duration: TimeInterval(animDur / 1000),
animationTimingFunction: CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut),
completionHandler: {
if let completion = completionHandler {
completion()
}
})
} else {
mapView.setVisibleCoordinates(coordinates, count: UInt(coordinates.count), edgePadding: padding, animated: true)
if let completion = completionHandler {
completion()
}
}

return nil
case "newLatLngZoom":
guard let coordinate = cameraUpdate[1] as? [Double] else { return nil }
guard let zoom = cameraUpdate[2] as? Double else { return nil }
Expand Down
104 changes: 98 additions & 6 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
private var interactiveFeatureLayerIds = Set<String>()
private var addedShapesByLayer = [String: MGLShape]()

private var lineLayers = [String : MGLLineStyleLayer]()
Copy link
Collaborator

Choose a reason for hiding this comment

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

same as with android

private var symbolLayers = [String : MGLSymbolStyleLayer]()
private var circleLayers = [String: MGLCircleStyleLayer]()

func view() -> UIView {
return mapView
}
Expand Down Expand Up @@ -321,27 +325,34 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let cameraUpdate = arguments["cameraUpdate"] as? [Any] else { return }
if let camera = Convert
.parseCameraUpdate(cameraUpdate: cameraUpdate, mapView: mapView)
.parseCameraUpdate(cameraUpdate: cameraUpdate, mapView: mapView, completionHandler: {
result(nil)
}, duration: arguments["duration"] as? TimeInterval)
{
mapView.setCamera(camera, animated: false)
}
result(nil)
case "camera#animate":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let cameraUpdate = arguments["cameraUpdate"] as? [Any] else { return }

if let camera = Convert
.parseCameraUpdate(cameraUpdate: cameraUpdate, mapView: mapView)
.parseCameraUpdate(cameraUpdate: cameraUpdate, mapView: mapView, completionHandler: {
result(nil)
}, duration: arguments["duration"] as? TimeInterval)
{
if let duration = arguments["duration"] as? TimeInterval {
mapView.setCamera(camera, withDuration: TimeInterval(duration / 1000),
animationTimingFunction: CAMediaTimingFunction(name: CAMediaTimingFunctionName
.easeInEaseOut))
result(nil)
.easeInEaseOut), completionHandler: {
result(nil)
})
} else {
mapView.setCamera(camera, animated: true)
result(nil)
}
}
result(nil)


case "symbolLayer#add":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
Expand Down Expand Up @@ -400,6 +411,51 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
case .success: result(nil)
case let .failure(error): result(error.flutterError)
}

case "lineLayer#setProperties":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let layerId = arguments["layerId"] as? String else { return }
guard let properties = arguments["properties"] as? [String: String] else { return }

let editResult = setLineLayerProperties(
layerId: layerId,
properties: properties
)

switch editResult {
case .success: result(nil)
case let .failure(error): result(error.flutterError)
}

case "symbolLayer#setProperties":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let layerId = arguments["layerId"] as? String else { return }
guard let properties = arguments["properties"] as? [String: String] else { return }

let editResult = setSymbolLayerProperties(
layerId: layerId,
properties: properties
)

switch editResult {
case .success: result(nil)
case let .failure(error): result(error.flutterError)
}

case "circleLayer#setProperties":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let layerId = arguments["layerId"] as? String else { return }
guard let properties = arguments["properties"] as? [String: String] else { return }

let editResult = setCircleLayerProperties(
layerId: layerId,
properties: properties
)

switch editResult {
case .success: result(nil)
case let .failure(error): result(error.flutterError)
}

case "fillLayer#add":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
Expand Down Expand Up @@ -756,7 +812,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
case let .failure(error): result(error.flutterError)
}

case "map#setVisibility":
case "style#setVisibility":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let layerId = arguments["layerId"] as? String else { return }
guard let isVisible = arguments["isVisible"] as? Bool else { return }
Expand Down Expand Up @@ -917,6 +973,9 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
if let layer = mapView.style?.layer(withIdentifier: layerId) {
mapView.style?.removeLayer(layer)
interactiveFeatureLayerIds.remove(layerId)
lineLayers.removeValue(forKey: layerId)
symbolLayers.removeValue(forKey: layerId)
circleLayers.removeValue(forKey: layerId)
}
}

Expand Down Expand Up @@ -1232,11 +1291,40 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
if enableInteraction {
interactiveFeatureLayerIds.insert(layerId)
}

symbolLayers[layerId] = layer;
}
}
return .success(())
}

func setLineLayerProperties(layerId: String, properties: [String: String]) -> Result<Void, MethodCallError> {
let layer : MGLLineStyleLayer? = lineLayers[layerId]
if let lineLayer = layer {
LayerPropertyConverter.addLineProperties(lineLayer: lineLayer, properties: properties)
}

return .success(())
}

func setSymbolLayerProperties(layerId: String, properties: [String: String]) -> Result<Void, MethodCallError> {
let layer : MGLSymbolStyleLayer? = symbolLayers[layerId]
if let symbolLayer = layer {
LayerPropertyConverter.addSymbolProperties(symbolLayer: symbolLayer, properties: properties)
}

return .success(())
}

func setCircleLayerProperties(layerId: String, properties: [String: String]) -> Result<Void, MethodCallError> {
let layer : MGLCircleStyleLayer? = circleLayers[layerId]
if let circleLayer = layer {
LayerPropertyConverter.addCircleProperties(circleLayer: circleLayer, properties: properties)
}

return .success(())
}

func addLineLayer(
sourceId: String,
layerId: String,
Expand Down Expand Up @@ -1274,6 +1362,8 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
if enableInteraction {
interactiveFeatureLayerIds.insert(layerId)
}

lineLayers[layerId] = layer;
}
}
return .success(())
Expand Down Expand Up @@ -1406,6 +1496,8 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
if enableInteraction {
interactiveFeatureLayerIds.insert(layerId)
}

circleLayers[layerId] = layer;
}
}
return .success(())
Expand Down
Loading
Loading