将动作点击分配给谷歌地图标记,swift 2

时间:2015-10-15 18:51:57

标签: google-maps-api-3 swift2 ios9 xcode7

我有一个谷歌地图标记,我希望当我点击标记发送给另一个viewController,或在我的地图上显示一个按钮,

let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2DMake(24.8236423, -107.4234671)
marker1.appearAnimation = kGMSMarkerAnimationPop
marker1.icon = UIImage(named: "flag_icon")
marker1.title = "Any title"
marker1.snippet = "Any text"
marker1.map = mapView

3 个答案:

答案 0 :(得分:4)

我解决了,这是点击功能

    //Market Tap Function
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
    let myFirstButton = UIButton()
    myFirstButton.setTitle("✸", forState: .Normal)
    myFirstButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
    myFirstButton.frame = CGRectMake(15, -50, 300, 500)
    myFirstButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)

    self.view.addSubview(myFirstButton)
    return true
}

答案 1 :(得分:2)

你可以将这些步骤用于swift 4

  1. 实施GMSMapViewDelegate
  2. 在didviewload()中将您的地图打到此委托,如下所示:mygooglemap.delegate = self
  3. 将此功能添加到您的班级:

    def main():
        def add(x,y): return x+y
        reduce(add, range(1, 11))
    

答案 2 :(得分:1)

对于Swift 5 +

您可以使用Delegete方法做到这一点

  1. 将地图视图删除分配给自己

  2. GMSMapViewDelegate对自己充满信心

这里是代码

1

import UIKit
import GoogleMaps
import GooglePlaces

class HomeVC: UIViewController {
 
override func viewDidLoad() {
     super.viewDidLoad()
     self.mapView.delegate = self
  }

}

2

extension HomeVC:GMSMapViewDelegate {
    
    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        print("Do what ever you want.")
        return true
    }
}