Files
mowesta/ios-app/app/View/PlaceMapView.swift
T
Marcus Handte 2c788e3329
LocationBasedServices/mowesta/pipeline/head This commit looks good
Add weather station configuration to ios app.
2022-11-03 16:47:26 +01:00

192 lines
6.5 KiB
Swift

//
// PlaceMapView.swift - The view to display a map of a place.
//
// Created by Marcus Handte on 20.07.22.
//
import SwiftUI
import Foundation
import LocosLabGeometry
import LocosLabLeafless
/// The actual view that renders the map of a place with the optional boundaries
struct PlaceMapView: UIViewRepresentable {
/// Dark mode or not
@Environment(\.colorScheme) var colorScheme
/// The initial center.
let center: Coordinate
// The initial zoom.
let zoom: Int
/// The boundary to render.
let boundary: [[Coordinate]]?
/// Called to create the underlying UIKit view.
///
/// - parameters:
/// - context: The context with the coordinator.
/// - returns: The map view.
func makeUIView(context: Context) -> MapView {
let v = MapView(frame:CGRect())
let o = v.options
o.pinchToRotate = false
o.pinchToZoom = false
o.doubleTapZoom = false
o.dragToMove = false
v.options = o
v.setCenter(center: Coordinate(longitude: center.longitude, latitude: center.latitude))
v.setZoom(zoom: zoom)
return v
}
/// Creates a coordinator that holds the view state.
///
/// - returns: The coordinator.
func makeCoordinator() -> PlaceMapCoordinator {
return PlaceMapCoordinator(self)
}
/// Updates the view when the state changed.
///
/// - parameters:
/// - uiView: The UiKit element with the map view.
/// - context: The context with the coordinator.
func updateUIView(_ uiView: MapView, context: Context) {
context.coordinator.update(uiView, self)
}
}
/// The coordinator that manages updates to the state of the underlying Leafless view.
class PlaceMapCoordinator {
/// The current center
var center: Coordinate
// The current zoom
var zoom: Int
// The boundary that is rendered
var boundary: [[Coordinate]]?
// The polyline representing the boundary
var polylines: [Polyline] = []
/// The current layer.
var tilelayer: TileLayer?
/// The color scheme of the layer.
var colorScheme: ColorScheme
/// The circle to mark the spot
var circle: LocosLabLeafless.Circle
/// Creates a new coordinator and initializes the variables
///
/// - parameters:
/// - mapView: The view to initialize the toggles.
init(_ mapView: PlaceMapView) {
self.center = mapView.center
self.zoom = mapView.zoom
self.colorScheme = mapView.colorScheme
let opts = CircleOptions()
opts.strokeColor = nil
opts.fillColor = UIColor(Color.accentColor)
opts.opacity = 0.5
circle = LocosLabLeafless.Circle(options: opts)
circle.radius = 10
circle.strokeWidth = 3
circle.unit = CircleUnit.pixel
circle.coordinate = mapView.center
}
/// Called to update the Leafless MapView on a change to the state of the view.
///
/// - parameters:
/// - mapView: The map view to apply changes.
/// - placeMapView: The map view that has changed.
func update(_ mapView: MapView, _ placeMapView: PlaceMapView) {
if tilelayer == nil || colorScheme != placeMapView.colorScheme {
if let t = tilelayer {
_ = mapView.removeLayer(layer: t)
_ = mapView.removeShape(shape: circle)
}
colorScheme = placeMapView.colorScheme
let url = colorScheme == .dark ? "https://www.mowesta.com/carta/tile/dark/{z}/{x}/{y}.png" : "https://www.mowesta.com/carta/tile/{z}/{x}/{y}.png"
let layerOptions = TileLayerOptions(url: url)
let tl = TileLayer(options: layerOptions)
mapView.addLayer(layer: tl);
tilelayer = tl
mapView.addShape(shape: circle)
}
if !equals(placeMapView.boundary, boundary) {
if !polylines.isEmpty {
polylines.forEach({ p in
_ = mapView.removeShape(shape: p)
})
polylines = []
}
if let cs = placeMapView.boundary {
var cords: [Coordinate] = []
cs.forEach({ b in
let options = PolylineOptions()
options.fillColor = UIColor(Color.accentColor)
options.fillOpacity = 0.7
options.strokeCap = .round
options.strokeJoin = .round
options.strokeWidth = 3
options.strokeOpacity = 0.9
if placeMapView.colorScheme == .dark {
options.strokeColor = UIColor(Color.accentColor)
} else {
options.strokeColor = UIColor(Color("PrimaryColor"))
}
let p = Polyline(coordinates: b, options: options)
mapView.addShape(shape: p)
polylines.append(p)
cords.append(contentsOf: b)
})
if !cords.isEmpty {
mapView.setViewport(bounds: Box(coordinates: cords), padding: MapPoint(x: 5, y: 5))
}
}
}
if polylines.isEmpty && (center.latitude != placeMapView.center.latitude
|| center.longitude != placeMapView.center.longitude
|| zoom != placeMapView.zoom) {
mapView.setCenter(center: placeMapView.center)
mapView.setZoom(zoom: placeMapView.zoom)
circle.coordinate = center
}
self.center = placeMapView.center
self.zoom = placeMapView.zoom
self.boundary = placeMapView.boundary
}
func equals(_ a: [[Coordinate]]?, _ b: [[Coordinate]]?) -> Bool {
if (a == nil && b != nil) || (a != nil && b == nil) {
return false
}
if let x = a, let y = b {
if x.count != y.count {
return false
}
for i in 0..<x.count {
if x[i].count != y[i].count {
return false
}
}
}
return a == nil && b == nil
}
/// Creates an color from an RGB value (0-255).
///
/// - parameters:
/// - r: The red component.
/// - g: The green component.
/// - b: The blue component.
func createColor(_ r: Int, _ g: Int, _ b: Int) -> UIColor {
return UIColor(red: CGFloat(Float(Double(r) / 255.0)), green: CGFloat(Float(Double(g) / 255.0)), blue: CGFloat(Float(Double(b) / 255.0)), alpha: 1)
}
}