diff options
Diffstat (limited to 'iosApp/iosApp/Map/MapViewController.swift')
-rw-r--r-- | iosApp/iosApp/Map/MapViewController.swift | 128 |
1 files changed, 116 insertions, 12 deletions
diff --git a/iosApp/iosApp/Map/MapViewController.swift b/iosApp/iosApp/Map/MapViewController.swift index 131a511..3c38b89 100644 --- a/iosApp/iosApp/Map/MapViewController.swift +++ b/iosApp/iosApp/Map/MapViewController.swift @@ -18,9 +18,23 @@ import UIKit import WhirlyGlobe import shared +import GEOSwift typealias MarkerCallback = (Int32?) -> () +extension Geometry { + func getCoordinates() -> [Point] { + switch self { + case let .polygon(polygon): + return polygon.exterior.points + case let .lineString(lineString): + return lineString.points + default: + return [] + } + } +} + class MapViewController: UIViewController { var markerCallback: MarkerCallback? = nil var mapLayer: MapLayer = MapLayer.companion.defaultLayer @@ -84,6 +98,12 @@ class MapViewController: UIViewController { } } + func display(geofences: [Geofence]) { + mapView.runOnInit { + self.mapView.display(geofences: geofences) + } + } + func focusOn(marker: Marker) { mapView.runOnInit { self.mapView.focusOn(marker: marker) @@ -205,13 +225,14 @@ class OurMaplyViewController: MaplyViewController { } let fontSize = 11.0 - let colorReport = UIColor.green - let colorLabel = UIColor.secondaryLabel - let colorLabelOutline = UIColor.systemBackground + let colorReport = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0) + let colorLabel = UIColor.darkGray + let colorLabelOutline = UIColor.white let vectorDesc: [AnyHashable : Any] = [ kMaplyColor: colorReport, - kMaplyVecWidth: 20.0 + kMaplyVecWidth: 15.0, + kMaplyWideVecImpl: kMaplyWideVecImplPerf ] let labelDesc: [AnyHashable : Any] = [ @@ -232,7 +253,7 @@ class OurMaplyViewController: MaplyViewController { // For reports, position, start and end icons must be different switch i { case markers.startIndex: type = .reportStart - case markers.endIndex: type = .reportEnd + case markers.endIndex - 1: type = .reportEnd default: type = .reportPosition } } else { @@ -245,8 +266,8 @@ class OurMaplyViewController: MaplyViewController { // For reports, position, start and end sizes must be different switch i { case markers.startIndex: size = 50.0 - case markers.endIndex: size = 50.0 - default: size = 25.0 + case markers.endIndex - 1: size = 50.0 + default: size = 26.0 } } screenMarker.size = CGSize(width: size, height: size) @@ -289,15 +310,19 @@ class OurMaplyViewController: MaplyViewController { "type": "FeatureCollection", "features": [ [ - "type": "LineString", - "coordinates": points.map({ point in - [point.x, point.y] - }) + "type": "Feature", + "properties": [], + "geometry": [ + "type": "LineString", + "coordinates": markers.map({ marker in + [marker.longitude, marker.latitude] + }) + ] ] ] ] if let vector = MaplyVectorObject(fromGeoJSONDictionary: geoJSON) { - if let objs = addVectors([vector], desc: vectorDesc, mode: .any) { + if let objs = addWideVectors([vector], desc: vectorDesc, mode: .any) { objects.append(objs) } } @@ -314,6 +339,85 @@ class OurMaplyViewController: MaplyViewController { } } + func display(geofences: [Geofence]) { + clear(geofences: true) + + let fontSize = 11.0 + let colorFill = UIColor(red: 0.10, green: 0.46, blue: 0.82, alpha: 1.00) + let colorLabel = UIColor(red: 0.10, green: 0.46, blue: 0.82, alpha: 1.00) + let colorLabelOutline = UIColor.white + + let vectorDesc: [AnyHashable : Any] = [ + kMaplyColor: colorFill, + kMaplyVecWidth: 12.0, + kMaplyWideVecImpl: kMaplyWideVecImplPerf + ] + + let labelDesc: [AnyHashable : Any] = [ + kMaplyFont: UIFont.boldSystemFont(ofSize: fontSize), + kMaplyTextColor: colorLabel, + kMaplyTextOutlineColor: colorLabelOutline, + kMaplyTextOutlineSize: 3.0 + ] + + let shapes = [MaplyShape]() + var vectors = [MaplyVectorObject]() + var labels = [MaplyScreenLabel]() + + geofences.forEach { geofence in + if let area = geofence.area { + print ("Draw geofence with area = \(area)") + if let geometry = try? Geometry(wkt: area) { + switch geometry { + case .polygon(_): + let geoJSON: [AnyHashable : Any] = [ + "type": "FeatureCollection", + "features": [ + [ + "type": "Feature", + "properties": [], + "geometry": [ + "type": "Polygon", + "coordinates": [ + geometry.getCoordinates().map { coordinate in + [coordinate.y, coordinate.x] + } + ] + ] + ] + ] + ] + if let vector = MaplyVectorObject(fromGeoJSONDictionary: geoJSON) { + vectors.append(vector) + } + default: + break + } + + if let centroid = try? geometry.centroid() { + let label = MaplyScreenLabel() + label.text = geofence.name + label.loc = MaplyCoordinateMakeWithDegrees(Float(centroid.y), + Float(centroid.x)) + label.layoutImportance = .infinity + + labels.append(label) + } + } + } + } + + if let objs = addShapes(shapes, desc: nil, mode: .any) { + geofenceObjects.append(objs) + } + if let objs = addWideVectors(vectors, desc: vectorDesc, mode: .any) { + geofenceObjects.append(objs) + } + if let objs = addScreenLabels(labels, desc: labelDesc, mode: .any) { + geofenceObjects.append(objs) + } + } + func setZoomLimits(minZoom: Int32, maxZoom: Int32) { setZoomLimitsMin( height(forMapScale: Float(truncating: |