Files
MapLibreParticleLab/platform/ios/Mapbox.playground/Contents.swift
mwilsnd 20d9e1028c Platform flattening overhaul (#1393)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bart Louwers <bart@emeel.net>
2023-07-26 17:50:34 +02:00

155 lines
5.4 KiB
Swift

import UIKit
import PlaygroundSupport
import Mapbox
let width: CGFloat = 700
let height: CGFloat = 800
class Responder: NSObject {
var mapView: MLNMapView?
@objc func togglePitch(sender: UISwitch) {
let camera = mapView!.camera
camera.pitch = sender.isOn ? 60 : 0
mapView!.setCamera(camera, animated: false)
}
}
//: A control panel
let panelWidth: CGFloat = 200
let panel = UIView(frame: CGRect(x: width - panelWidth, y: 0, width: 200, height: 100))
panel.alpha = 0.8
panel.backgroundColor = .white
// Delete markers
let deleteSwitchLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
deleteSwitchLabel.adjustsFontSizeToFitWidth = true
deleteSwitchLabel.text = "Delete Markers"
let deleteMarkerSwitchView = UISwitch(frame: CGRect(x: panelWidth - panelWidth / 2.0, y:0, width: 100, height: 50))
panel.addSubview(deleteSwitchLabel)
panel.addSubview(deleteMarkerSwitchView)
// Hide markers
let hideSwitchLabel = UILabel(frame: CGRect(x: 0, y: 30, width: 100, height: 30))
hideSwitchLabel.adjustsFontSizeToFitWidth = true
hideSwitchLabel.text = "Hide Markers"
let hideMarkerSwitchView = UISwitch(frame: CGRect(x: panelWidth - panelWidth / 2.0, y: 30, width: 100, height: 50))
panel.addSubview(hideSwitchLabel)
panel.addSubview(hideMarkerSwitchView)
// Pitch map
let pitchLabel = UILabel(frame: CGRect(x: 0, y: 60, width: 100, height: 30))
pitchLabel.text = "Pitch"
let pitchSwitch = UISwitch(frame: CGRect(x: panelWidth-panelWidth / 2.0, y: 60, width: 100, height: 50))
let responder = Responder()
pitchSwitch.addTarget(responder, action: #selector(responder.togglePitch(sender:)), for: .valueChanged)
panel.addSubview(pitchLabel)
panel.addSubview(pitchSwitch)
//: # Mapbox Maps
/*:
Put your access token into a plain text file called `token`. Then select the “token” placeholder below, go to Editor ‣ Insert File Literal, and select the `token` file.
*/
var apiKey = try String(contentsOfURL: <#token#>)
MLNSettings.apiKey = apiKey
class PlaygroundAnnotationView: MLNAnnotationView {
override func prepareForReuse() {
isHidden = hideMarkerSwitchView.isOn
}
}
//: Define a map delegate
class MapDelegate: NSObject, MLNMapViewDelegate {
var annotationViewByAnnotation = [MLNPointAnnotation: PlaygroundAnnotationView]()
func mapView(_ mapView: MLNMapView, viewFor annotation: MLNAnnotation) -> MLNAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotation") as? PlaygroundAnnotationView
if (annotationView == nil) {
let av = PlaygroundAnnotationView(reuseIdentifier: "annotation")
av.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
av.centerOffset = CGVector(dx: -15, dy: -15)
let centerView = UIView(frame: av.bounds.insetBy(dx: 3, dy: 3))
centerView.backgroundColor = .white
av.addSubview(centerView)
av.backgroundColor = .purple
annotationView = av
} else {
annotationView!.subviews.first?.backgroundColor = .green
}
annotationViewByAnnotation[annotation as! MLNPointAnnotation] = annotationView
return annotationView
}
func mapView(_ mapView: MLNMapView, didSelect annotation: MLNAnnotation) {
let pointAnnotation = annotation as! MLNPointAnnotation
let annotationView: PlaygroundAnnotationView = annotationViewByAnnotation[pointAnnotation]!
for view in annotationViewByAnnotation.values {
view.layer.zPosition = -1
}
annotationView.layer.zPosition = 1
UIView.animate(withDuration: 1.25, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.6, options: .curveEaseOut, animations: {
annotationView.transform = CGAffineTransform(scaleX: 1.8, y: 1.8)
}) { _ in
annotationView.transform = CGAffineTransform(scaleX: 1, y: 1)
if deleteMarkerSwitchView.isOn {
mapView.removeAnnotation(pointAnnotation)
return
}
if hideMarkerSwitchView.isOn {
annotationView.isHidden = true
}
}
}
@objc func handleTap(press: UILongPressGestureRecognizer) {
let mapView: MLNMapView = press.view as! MLNMapView
let isRecognized = press.state == .recognized
if (isRecognized) {
let annotation = MLNPointAnnotation()
annotation.title = "Dropped Marker"
annotation.coordinate = mapView.convert(press.location(in: mapView), toCoordinateFrom: mapView)
mapView.addAnnotation(annotation)
mapView.showAnnotations([annotation], animated: true)
}
}
}
//: Create a map and its delegate
let mapView = MLNMapView(frame: CGRect(x: 0, y: 0, width: width, height: height))
mapView.frame = CGRect(x: 0, y: 0, width: width, height: height)
PlaygroundPage.current.liveView = mapView
let mapDelegate = MapDelegate()
mapView.delegate = mapDelegate
responder.mapView = mapView
let tapGesture = UILongPressGestureRecognizer(target: mapDelegate, action: #selector(mapDelegate.handleTap))
mapView.addGestureRecognizer(tapGesture)
//: Zoom in to a location
mapView.setCenter(CLLocationCoordinate2D(latitude: 37.174057, longitude: -104.490984), zoomLevel: 12, animated: false)
//: Add control panel
mapView.addSubview(panel)