【问题标题】:Swift: How to set functions to each specific marker?Swift:如何为每个特定标记设置功能?
【发布时间】:2017-05-12 13:36:56
【问题描述】:

在以下代码中,我需要将每个特定标记设置为一个函数。如何将功能设置为:点击 MarkerInfoWindow 时,打开 URL? URL 需要针对每个标记都是特定的。

想法是当 MarkerInfoWindow 被点击为 marker1 时,它应该显示这个 marker1 的导航。

问题是我如何重写此代码以使函数特定于每个标记

标记 1:

let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: 55.6726299, longitude: 12.5662175)
marker1.title = "Københavns Hovedbanegård"
marker1.snippet = "Press for navigation"
marker1.tracksViewChanges = true
marker1.opacity = 0.9
marker1.icon = UIImage(named: "BCmarker")
marker1.appearAnimation = kGMSMarkerAnimationNone
marker1.map = self.googleMapsView

标记 2:

let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude: 55.68, longitude: 12.55)
marker2.title = "Test Marker2"
marker2.snippet = "Press for navigation"
marker2.tracksViewChanges = true
marker2.opacity = 0.9
marker2.icon = UIImage(named: "BCmarker")
marker2.appearAnimation = kGMSMarkerAnimationNone
marker2.map = self.googleMapsView

每个标记具体应分配的功能:

func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
let testURL = URL(string: "comgooglemaps-x-callback://")!
if UIApplication.shared.canOpenURL(testURL) {
    let directionsRequest = "comgooglemaps-x-callback://" +
    "?daddr=55.6726299,12.5662175&directionsmode=walking&zoom=17"

    let directionsURL = URL(string: directionsRequest)!
    UIApplication.shared.openURL(directionsURL)
} else {
    UIApplication.shared.openURL(NSURL(string: "http://maps.apple.com/?address=1600,PennsylvaniaAve.,20500")! as URL)
}

谢谢!

在@Sweeper 的回答之后,代码如下所示: 导入 UIKit 导入谷歌地图 导入 GooglePlaces

class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, GMSAutocompleteViewControllerDelegate {

// OUTLETS!
@IBOutlet weak var googleMapsView: GMSMapView!

// VARIABLES!
var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    // GET LOCATION WHILE USING APP
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    locationManager.startMonitoringSignificantLocationChanges()

    initGoogleMaps()
}


    // START GOOGLE MAPS!
func initGoogleMaps() {

    let zoomCamera = GMSCameraUpdate.zoomIn()
    googleMapsView.animate(with: zoomCamera)

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: 55.6760968, longitude: 12.568337100000008, zoom: 12.5)
    _ = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    self.googleMapsView.camera = camera

    // CREATE FIND LOCATION BUTTON??
    self.googleMapsView.delegate = self
    self.googleMapsView.isMyLocationEnabled = true
    self.googleMapsView.settings.myLocationButton = true

    // MARKERS
    let marker1 = GMSMarker()
    marker1.position = CLLocationCoordinate2D(latitude: 55.6726299, longitude: 12.5662175)
    marker1.title = "Københavns Hovedbanegård"
    marker1.snippet = "Tryk for at få navigation"
    marker1.tracksViewChanges = true
    marker1.opacity = 0.9
    marker1.icon = UIImage(named: "BCmarker")
    marker1.appearAnimation = kGMSMarkerAnimationNone
    marker1.map = self.googleMapsView

    let marker2 = GMSMarker()
    marker2.position = CLLocationCoordinate2D(latitude: 55.68, longitude: 12.55)
    marker2.title = "Test Marker2"
    marker2.snippet = "Tryk for at få navigation"
    marker2.tracksViewChanges = true
    marker2.opacity = 0.9
    marker2.icon = UIImage(named: "BCmarker")
    marker2.appearAnimation = kGMSMarkerAnimationNone
    marker2.map = self.googleMapsView

    let markerFunctions: [GMSMarker: (() -> Void)] = [
        marker1: { print("Test1") },
            marker2: { print("Test2") }
    ]

}

// ...something else about Google Places

【问题讨论】:

    标签: swift function google-maps navigation google-maps-markers


    【解决方案1】:

    mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) 将在 任何 标记的信息窗口被点击时被调用。您希望获得一个仅在点击 特定 标记的信息窗口时才会调用的特定标记。希望我正确理解了您的问题。

    很遗憾,Google Maps API 中没有这样的委托方法。

    好吧,简单的解决方法就是使用 if 语句来检查它是 marker1 还是 marker2

    func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
        if marker == marker1 { // remember to declare marker 1 and 2 at class level!
            // do stuff
        } else if marker == marker2 {
            // do other stuff
        }
    }
    

    或者,您可以将您希望每个标记执行的操作放入字典中:

    let markerFuntions: [GMSMarker: (() -> Void)] = [
        marker1: { // do stuff }
        marker2: { // do other stuff }
    ]
    

    然后您可以像这样编写委托方法:

    func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
        markerFunctions[marker]?()
    }
    

    编辑:我试图解决你的代码的问题,这就是我得到的:

    class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, GMSAutocompleteViewControllerDelegate {
    
    // OUTLETS!
    @IBOutlet weak var googleMapsView: GMSMapView!
    
    // VARIABLES!
    var locationManager = CLLocationManager()
    var marker1: GMSMarker!
    var marker2: GMSMarker!
    var markerFunctions: [GMSMarker: (() -> Void)]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // GET LOCATION WHILE USING APP
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        locationManager.startMonitoringSignificantLocationChanges()
    
        initGoogleMaps()
    }
    
    
        // START GOOGLE MAPS!
    func initGoogleMaps() {
    
        let zoomCamera = GMSCameraUpdate.zoomIn()
        googleMapsView.animate(with: zoomCamera)
    
        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
        let camera = GMSCameraPosition.camera(withLatitude: 55.6760968, longitude: 12.568337100000008, zoom: 12.5)
        _ = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        self.googleMapsView.camera = camera
    
        // CREATE FIND LOCATION BUTTON??
        self.googleMapsView.delegate = self
        self.googleMapsView.isMyLocationEnabled = true
        self.googleMapsView.settings.myLocationButton = true
    
        // MARKERS
        marker1 = GMSMarker()
        marker1.position = CLLocationCoordinate2D(latitude: 55.6726299, longitude: 12.5662175)
        marker1.title = "Københavns Hovedbanegård"
        marker1.snippet = "Tryk for at få navigation"
        marker1.tracksViewChanges = true
        marker1.opacity = 0.9
        marker1.icon = UIImage(named: "BCmarker")
        marker1.appearAnimation = kGMSMarkerAnimationNone
        marker1.map = self.googleMapsView
    
        marker2 = GMSMarker()
        marker2.position = CLLocationCoordinate2D(latitude: 55.68, longitude: 12.55)
        marker2.title = "Test Marker2"
        marker2.snippet = "Tryk for at få navigation"
        marker2.tracksViewChanges = true
        marker2.opacity = 0.9
        marker2.icon = UIImage(named: "BCmarker")
        marker2.appearAnimation = kGMSMarkerAnimationNone
        marker2.map = self.googleMapsView
    
        markerFunctions = [
            marker1: { print("Test1") },
            marker2: { print("Test2") }
        ]
    
    }
    
    func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
        markerFunctions[marker]?()
    }
    
    // ...something else about Google Places
    

    【讨论】:

    • 嗨@Sweeper 我在标记下方添加了“let markerFunctions ....”并在“AppDelegate 文件”中添加了该函数,但错误:“使用未解析的标识符'markerFuntions'...出现。我该怎么做才能避免这种情况?(所有目标都在“目标成员资格”中选中。)
    • @ChristofferSkov 为什么不将各个函数声明在与地图视图委托相同的类中?您可以编辑问题以添加相关代码吗?
    • 我已经编辑了这个问题,所以你可以看到全文。请注意,该函数仍在“AppDelegate”文件中。这里代码如下: func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) { markerFunctions[marker]?() }
    • 我忘记标记你了。
    • @ChristofferSkov 您需要将标记的声明和字典移到initGoogleMaps 之外。另外,将ViewController 设置为地图的委托,而不是AppDelegate。这是基本的变量范围。我认为你应该先打好编程的基础,然后再开始做谷歌地图。
    猜你喜欢
    • 1970-01-01
    • 2013-08-31
    • 2020-04-13
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多