Я поместил свой mapView в качестве свойства в структуру, и теперь я не могу использовать маркерные события, потому что я не знаю, как ссылаться на правильный mapView, который находится внутри структуры.
Я пытаюсьчтобы выяснить, как я смогу связать «прослушиватель», который нажал на маркер, к правильному mapView, который я настроил.Я действительно борюсь с этим.
//Import Packages
import Foundation
import UIKit
import GoogleMaps
import FirebaseDatabase
//Set up Camera View to point to Town Center
let camera = GMSCameraPosition.camera(withLatitude: 50.535712, longitude: 0.078717, zoom: 15.4)
struct MapViewSetup{
static let locationManager = CLLocationManager()
static let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
}
//Create an array of these markers from 'GlobalVars.swift'
var markers = [Markers.marker1, Markers.marker2, Markers.marker3, Markers.marker4, Markers.marker5, Markers.marker6, Markers.marker7, Markers.marker8, Markers.marker9, Markers.marker10, Markers.marker11]
class ViewController: UIViewController, GMSMapViewDelegate {
override func loadView() {
//Call the map
view = MapViewSetup.mapView
//Call each marker one by one, this is done so program can know which array index is what.
showmarkers(marker_number: 0)
showmarkers(marker_number: 1)
showmarkers(marker_number: 2)
showmarkers(marker_number: 3)
showmarkers(marker_number: 4)
showmarkers(marker_number: 5)
showmarkers(marker_number: 6)
showmarkers(marker_number: 7)
showmarkers(marker_number: 8)
showmarkers(marker_number: 9)
showmarkers(marker_number: 10)
MapViewSetup.locationManager.delegate = self
MapViewSetup.locationManager.requestWhenInUseAuthorization()
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print("tapped on marker")
if marker.title == "Me" {
print("handle specific marker")
}
return true
}
//This function calls database and gets lon, lat and name, and puts it into the corresponding array. All of them can't be called marker because it won't know how to differentiate between each marker.
func getmarker(markernumber : Int) {
let ref = Database.database().reference()
ref.child("/markers/\(markernumber)/coordinates/0").observeSingleEvent(of: .value) { (snapshot) in
let lon = snapshot.value as! Double
ref.child("/markers/\(markernumber)/coordinates/1").observeSingleEvent(of: .value) { (snapshot) in
let lat = snapshot.value as! Double
ref.child("/markers/\(markernumber)/properties/title").observeSingleEvent(of: .value) { (snapshot) in
let title = snapshot.value as! String
// ref.child("/events/0/activity").observeSingleEvent(of: .value) { (snapshot) in
// let activity = snapshot.value as! String
// print(activity)
//print(lon, lat, title)l
//Assigns marker information to corressponding marker.
markers[markernumber].position = CLLocationCoordinate2D(latitude: lat, longitude: lon)
markers[markernumber].title = title
markers[markernumber].snippet = "Greenspace Certified!"
markers[markernumber].map = MapViewSetup.mapView
markers[markernumber].icon = UIImage(named: "greenspace_marker")
//print(markers[markernumber])
}
}
}
}
//This calls each marker and initiates getmarker func.
func showmarkers(marker_number: Int) {
getmarker(markernumber: marker_number)
}
}
// MARK: - CLLocationManagerDelegate
//1
extension ViewController: CLLocationManagerDelegate {
// 2
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
// 3
guard status == .authorizedWhenInUse else {
return
}
// 4
MapViewSetup.locationManager.startUpdatingLocation()
//5
MapViewSetup.mapView.isMyLocationEnabled = true
MapViewSetup.mapView.settings.myLocationButton = true
}
// 6
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
// 7
MapViewSetup.mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15.7, bearing: 0, viewingAngle: 0)
// 8
MapViewSetup.locationManager.stopUpdatingLocation()
}
}