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
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 @@ -36,6 +36,14 @@ static CameraPosition toCameraPosition(Object o) {
builder.target(toLatLng(data.get("target")));
builder.tilt(toFloat(data.get("tilt")));
builder.zoom(toFloat(data.get("zoom")));
HashMap paddingMap = ((HashMap) data.get("padding"));
if(paddingMap != null) {
builder.padding(toDouble(paddingMap.get("left")),
toDouble(paddingMap.get("top")),
toDouble(paddingMap.get("right")),
toDouble(paddingMap.get("bottom")));
}

return builder.build();
}

Expand Down
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
Loading