如何使用 swift 4 在 GMSMapView 上添加按钮
How to add button on GMSMapView using swift 4
这是我的视图控制器,即使我向 CGRect 添加值它也不起作用
import UIKit
import GoogleMaps
class ViewController: UIViewController,CLLocationManagerDelegate,
GMSMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 52.649030,
longitude: 1.174155, zoom: 14)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 52.649030,
longitude: 1.174155)
marker.title = "marker1"
marker.snippet = "city"
marker.map = mapView
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
self.view.addSubview(button)
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print(marker.position)
return true
}
}
所以,我的代码有没有错误?
谢谢
你的代码没有问题。按钮实际上在那里。
如果你检查图层,你可以在左上角看到它,像这样:
只需添加按钮 title/title 颜色。或背景颜色,您会看到它。
let button = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
button.setTitle("Button", for: .normal)
button.setTitleColor(.red, for: .normal)
button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
self.view.addSubview(button)
@objc func handleTap(_ sender: UIButton) {
print("You tapped a button")
}
这是我的视图控制器,即使我向 CGRect 添加值它也不起作用
import UIKit
import GoogleMaps
class ViewController: UIViewController,CLLocationManagerDelegate,
GMSMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 52.649030,
longitude: 1.174155, zoom: 14)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 52.649030,
longitude: 1.174155)
marker.title = "marker1"
marker.snippet = "city"
marker.map = mapView
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
self.view.addSubview(button)
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print(marker.position)
return true
}
}
所以,我的代码有没有错误? 谢谢
你的代码没有问题。按钮实际上在那里。 如果你检查图层,你可以在左上角看到它,像这样:
只需添加按钮 title/title 颜色。或背景颜色,您会看到它。
let button = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
button.setTitle("Button", for: .normal)
button.setTitleColor(.red, for: .normal)
button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
self.view.addSubview(button)
@objc func handleTap(_ sender: UIButton) {
print("You tapped a button")
}