Skip to content

Commit

Permalink
Set AutoPanMode (#79)
Browse files Browse the repository at this point in the history
* initial test

* initial test

* add set autopan mode

* remove debug code

* add android code

* refactor code a bit

* add mapoptions

* debug json parsing

* remove autoPanMode in init

* expose wanderExtentFactor and getter of autoPanMode

* fix double <-> float

* remove autoPanMode from mapOptions

* remove autoPanMode

* add example

* small changes from feedback

* add readme

* rollback autoformat
  • Loading branch information
sbergmair authored Aug 22, 2024
1 parent 1681884 commit 28a0c43
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 13 deletions.
1 change: 1 addition & 0 deletions arcgis_map_sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Checkout the example app `example/lib/main.dart` for more details.
| toggleBaseMap ||||
| moveCamera ||||
| moveCameraToPoints | |||
| use AutoPanMode | |||
| exportImage | |||
| zoomIn ||||
| zoomOut ||||
Expand Down
20 changes: 20 additions & 0 deletions arcgis_map_sdk/lib/src/arcgis_location_display.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ class ArcgisLocationDisplay {
return ArcgisMapPlatform.instance.stopLocationDisplayDataSource(_mapId!);
}

void setAutoPanMode(AutoPanMode autoPanMode) {
_assertAttached();
ArcgisMapPlatform.instance.setAutoPanMode(autoPanMode.name, _mapId!);
}

Future<AutoPanMode> getAutoPanMode() {
_assertAttached();
return ArcgisMapPlatform.instance.getAutoPanMode(_mapId!);
}

void setWanderExtentFactor(double factor) {
_assertAttached();
return ArcgisMapPlatform.instance.setWanderExtentFactor(factor, _mapId!);
}

Future<double> getWanderExtentFactor() {
_assertAttached();
return ArcgisMapPlatform.instance.getWanderExtentFactor(_mapId!);
}

Future<void> setDefaultSymbol(Symbol symbol) {
_assertAttached();
return ArcgisMapPlatform.instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.esri.arcgisruntime.mapping.Viewpoint
import com.esri.arcgisruntime.mapping.view.AnimationCurve
import com.esri.arcgisruntime.mapping.view.Graphic
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay
import com.esri.arcgisruntime.mapping.view.LocationDisplay.AutoPanMode
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.symbology.Symbol
import com.google.gson.reflect.TypeToken
Expand Down Expand Up @@ -160,6 +161,10 @@ internal class ArcgisMapView(
result
)

"set_auto_pan_mode" -> onSetAutoPanMode(call = call, result = result)
"get_auto_pan_mode" -> onGetAutoPanMode(call = call, result = result)
"set_wander_extent_factor" -> onSetWanderExtentFactor(call = call, result = result)
"get_wander_extent_factor" -> onGetWanderExtentFactor(call = call, result = result)
"location_display_set_accuracy_symbol" -> onSetLocationDisplayAccuracySymbol(
call,
result
Expand Down Expand Up @@ -267,6 +272,75 @@ internal class ArcgisMapView(
}
}

private fun onSetAutoPanMode(
call: MethodCall,
result: MethodChannel.Result
) {
try {
val mode = call.arguments as String?
if (mode == null) {
result.error(
"missing_data",
"Invalid argument, expected an autoPanMode as string",
null,
)
return
}
val autoPanMode = mode.autoPanModeFromString()
if (autoPanMode != null) {
mapView.locationDisplay.autoPanMode = autoPanMode
result.success(true)
} else {
result.error(
"invalid_data",
"Invalid argument, expected an AutoPanMode but got $mode",
null,
)
}
} catch (e: Throwable) {
result.finishWithError(e, "Setting AutoPanMode failed.")
}
}

private fun onGetAutoPanMode(
call: MethodCall,
result: MethodChannel.Result
) {
try {
return result.success(mapView.locationDisplay.autoPanMode.name)
} catch (e: Throwable) {
result.finishWithError(e, "Getting AutoPanMode failed.")
}
}

private fun onSetWanderExtentFactor(
call: MethodCall,
result: MethodChannel.Result
) {
try {
val factor = call.arguments as Double?
if (factor == null) {
result.error(
"missing_data",
"Invalid argument, expected an WanderExtentFactor as Float",
null,
)
return
}
mapView.locationDisplay.wanderExtentFactor = factor.toFloat()
result.success(true)
} catch (e: Throwable) {
result.finishWithError(e, "Setting WanderExtentFactor failed.")
}
}

private fun onGetWanderExtentFactor(
call: MethodCall,
result: MethodChannel.Result
) {
return result.success(mapView.locationDisplay.wanderExtentFactor)
}

private fun onSetLocationDisplayDataSourceType(call: MethodCall, result: MethodChannel.Result) {
if (mapView.locationDisplay.locationDataSource.status == LocationDataSource.Status.STARTED) {
result.error(
Expand Down Expand Up @@ -296,7 +370,11 @@ internal class ArcgisMapView(
}
}

else -> result.error("invalid_data", "Unknown data source type ${call.arguments}", null)
else -> result.error(
"invalid_data",
"Unknown data source type ${call.arguments}",
null,
)
}

}
Expand Down Expand Up @@ -640,3 +718,10 @@ private fun LoadStatusChangedEvent.jsonValue() = when (newLoadStatus) {
else -> "unknown"
}

private fun String.autoPanModeFromString() = when (this) {
"compassNavigation" -> AutoPanMode.COMPASS_NAVIGATION
"navigation" -> AutoPanMode.NAVIGATION
"recenter" -> AutoPanMode.RECENTER
"off" -> AutoPanMode.OFF
else -> null
}
83 changes: 79 additions & 4 deletions arcgis_map_sdk_ios/ios/Classes/ArcgisMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class ArcgisMapView: NSObject, FlutterPlatformView {
}
map.basemap = AGSBasemap(baseLayers: layers, referenceLayers: nil)
}


map.minScale = convertZoomLevelToMapScale(mapOptions.minZoom)
map.maxScale = convertZoomLevelToMapScale(mapOptions.maxZoom)
Expand Down Expand Up @@ -160,6 +159,10 @@ class ArcgisMapView: NSObject, FlutterPlatformView {
case "location_display_set_data_source_type" : onSetLocationDisplayDataSourceType(call, result)
case "update_is_attribution_text_visible": onUpdateIsAttributionTextVisible(call, result)
case "export_image" : onExportImage(result)
case "set_auto_pan_mode": onSetAutoPanMode(call, result)
case "get_auto_pan_mode": onGetAutoPanMode(call, result)
case "set_wander_extent_factor": onSetWanderExtentFactor( call, result)
case "get_wander_extent_factor": onGetWanderExtentFactor( call, result)
default:
result(FlutterError(code: "Unimplemented", message: "No method matching the name \(call.method)", details: nil))
}
Expand Down Expand Up @@ -459,7 +462,6 @@ class ArcgisMapView: NSObject, FlutterPlatformView {
operationWithSymbol(call, result) { mapView.locationDisplay.pingAnimationSymbol = $0 }
}


private func onSetLocationDisplayUseCourseSymbolOnMove(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
guard let active = call.arguments as? Bool else {
result(FlutterError(code: "missing_data", message: "Invalid arguments.", details: nil))
Expand All @@ -486,6 +488,45 @@ class ArcgisMapView: NSObject, FlutterPlatformView {
result(true)
}

private func onSetAutoPanMode(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
guard let mode = call.arguments as? String else {
result(FlutterError(code: "missing_data", message: "Invalid argument, expected an AutoPanMode as string.", details: nil))
return
}

guard let autoPanMode = mode.autoPanModeFromString() else {
result(FlutterError(code: "invalid_data", message: "Invalid argument, expected an AutoPanMode but got \(mode).", details: nil))
return
}

mapView.locationDisplay.autoPanMode = autoPanMode
result(true)
}

private func onGetAutoPanMode(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
// autoPanMode.rawValue is any of [0; 3]:
// https://developers.arcgis.com/ios/api-reference/_a_g_s_location_display_8h.html
guard let stringName = mapView.locationDisplay.autoPanMode.toName() else {
result(FlutterError(code: "invalid_data", message: "AutoPanMode has invalid state", details: nil))
return
}
return result(stringName)
}

private func onSetWanderExtentFactor(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
guard let factor = call.arguments as? Double else {
result(FlutterError(code: "missing_data", message: "Invalid argument, expected an WanderExtentFactor as Double.", details: nil))
return
}

mapView.locationDisplay.wanderExtentFactor = Float(factor)
result(true)
}

private func onGetWanderExtentFactor(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
return result(mapView.locationDisplay.wanderExtentFactor)
}

private func onSetLocationDisplayDataSourceType(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
if(mapView.locationDisplay.dataSource.status == .started) {
result(FlutterError(code: "invalid_state", message: "Current data source is running. Make sure to stop it before setting a new data source", details: nil))
Expand Down Expand Up @@ -518,14 +559,14 @@ class ArcgisMapView: NSObject, FlutterPlatformView {
mapView.isAttributionTextVisible = isVisible
result(true)
}

private func onExportImage(_ result: @escaping FlutterResult) {
mapView.exportImage { image, error in
if let error = error {
result(FlutterError(code: "export_error", message: error.localizedDescription, details: nil))
return
}

if let image = image, let imageData = image.pngData() {
result(FlutterStandardTypedData(bytes: imageData))
} else {
Expand Down Expand Up @@ -727,3 +768,37 @@ extension AGSLoadStatus {
}
}
}

extension String {
func autoPanModeFromString() -> AGSLocationDisplayAutoPanMode? {
switch self {
case "compassNavigation":
return .compassNavigation
case "navigation":
return .navigation
case "recenter":
return .recenter
case "off":
return .off
default:
return nil
}
}
}

extension AGSLocationDisplayAutoPanMode {
func toName() -> String? {
switch self {
case .off:
return "off"
case .recenter:
return "recenter"
case .navigation:
return "navigation"
case .compassNavigation:
return "compassNavigation"
@unknown default:
return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ struct ArcgisMapOptions: Codable {
let yMax: Int
let isAttributionTextVisible: Bool?
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ class MethodChannelArcgisMapPlugin extends ArcgisMapPlatform {
throw UnimplementedError('setMouseCursor() has not been implemented');
}

@override
void setAutoPanMode(String autoPanMode, int mapId) {
_methodChannelBuilder(mapId).invokeMethod("set_auto_pan_mode", autoPanMode);
}

@override
Future<AutoPanMode> getAutoPanMode(int mapId) {
return _methodChannelBuilder(mapId)
.invokeMethod<String>("get_auto_pan_mode")
.then((value) => AutoPanMode.values.byName(value!));
}

@override
void setWanderExtentFactor(double factor, int mapId) {
_methodChannelBuilder(mapId).invokeMethod(
"set_wander_extent_factor",
factor,
);
}

@override
Future<double> getWanderExtentFactor(int mapId) {
return _methodChannelBuilder(mapId)
.invokeMethod<double>("get_wander_extent_factor")
.then((value) => value!);
}

@override
void updateGraphicSymbol({
required int mapId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export 'package:arcgis_map_sdk_platform_interface/src/models/user_position.dart'
export 'package:arcgis_map_sdk_platform_interface/src/models/view_position.dart';
export 'package:arcgis_map_sdk_platform_interface/src/types/arcgis_map_options.dart';
export 'package:arcgis_map_sdk_platform_interface/src/types/attributes.dart';
export 'package:arcgis_map_sdk_platform_interface/src/types/auto_pan_mode.dart';
export 'package:arcgis_map_sdk_platform_interface/src/types/bounding_box.dart';
export 'package:arcgis_map_sdk_platform_interface/src/types/default_widget.dart';
export 'package:arcgis_map_sdk_platform_interface/src/types/elevation_mode.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ class ArcgisMapPlatform extends PlatformInterface {
throw UnimplementedError('setMouseCursor() has not been implemented');
}

void setAutoPanMode(String autoPanMode, int mapId) {
throw UnimplementedError('setAutoPanMode() has not been implemented');
}

Future<AutoPanMode> getAutoPanMode(int mapId) {
throw UnimplementedError('getAutoPanMode() has not been implemented');
}

void setWanderExtentFactor(double factor, int mapId) {
throw UnimplementedError(
'setWanderExtentFactor() has not been implemented',
);
}

Future<double> getWanderExtentFactor(int mapId) {
throw UnimplementedError(
'getWanderExtentFactor() has not been implemented',
);
}

void updateGraphicSymbol({
required int mapId,
required String layerId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum AutoPanMode { off, compassNavigation, navigation, recenter }
Loading

0 comments on commit 28a0c43

Please sign in to comment.