使用 Swift 在 GMSMapView(Google 地图)上添加按钮

3

由于我是新手,对Swift编程和iOS开发都不太熟悉,我在将按钮置于Google地图顶部时遇到了问题。

我一直在使用Cocopods的Google Maps SDK for iOS,并参考了Google Map API指南中的示例。

override func viewDidLoad() {
    super.viewDidLoad()

    var camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6)
    var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    mapView.settings.myLocationButton = true;
    self.view = mapView

    var marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

}

我想把按钮放在地图上方。 enter image description here
6个回答

9

在添加地图后,只需创建一个UIButton并将其添加到您的视图中,在您的viewDidLoad中的最后一件事情应该是可以的。

let button = UIButton(frame: whereYouWant)
self.view.addSubview(button)

1
很高兴知道。请随意将答案标记为正确的答案 :) - Felipe Cypriano
您的评论缺乏足够的信息,无法提供帮助。 - Felipe Cypriano
菲利普,你能看到我的问题吗?(https://stackoverflow.com/questions/52440728/add-some-button-to-gmsmapview-google-map-and-change-the-image-button?noredirect=1#comment91824428_52440728) - Firuma

3

尝试在您的ViewController类中做这样的事情,可能是在viewDidLoad()方法中:

假设self.view是GmsMapView的实例,像这样:
self.view = GMSMapView.map(withFrame: .zero, camera: someCamera)

let btn: UIButton = UIButton(type: UIButtonType.roundedRect)
btn.frame = CGRect(x: 100, y: 100, width: 100, height: 30)
btn.setTitle("MyButton", for: UIControlState.normal)
self.view.addSubview(btn)

For XCode 11

let btn: UIButton = UIButton(type: UIButton.ButtonType.roundedRect)
btn.frame = CGRect(x: 100, y: 100, width: 100, height: 30)
btn.setTitle("MyButton", for: UIControl.State.normal)
self.view.addSubview(btn)

3
假设你有一个名为“googleMapView”的谷歌地图视图和一个名为“yourButton”的UIButton。请确保通过以下方式将GooglemapView插入按钮下方:
self.view.insertSubview(googleMapView, atIndex: 0)
self.view.insertSubview(yourButton, atIndex: 1)

1
我尝试了几种选项,但没有结果。最后发现在实现 GoogleMap 在一个 view 上后,需要在其之后再实现一个 button 或其他任何东西,就像这样:
    override func viewDidLoad() {
    super.viewDidLoad()

    //Google Maps

    // Create a GMSCameraPosition that tells the map to display the
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

    // Add a button
    let revealButton = UIButton()
    let btnImage = UIImage(named: "reveal-icon")
    revealButton.setImage(btnImage, for: .normal)
    revealButton.frame = CGRect(x: 16, y: 38, width: 48, height: 41)
    self.view.addSubview(revealButton)

    // RevealViewController for button.
    let revealViewController = self.revealViewController()
    revealButton.addTarget(revealViewController,
                         action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())


}

0
self.okBtnOutlet.addTarget(self, action: #selector(placeMarkerPointOnDrag), for: .touchUpInside)

@objc  func placeMarkerPointOnDrag(){
    self.placeMarkerOnMapOnButtonTap(coordinate: self.draggingCoordinate)
}

func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
    print("End dragging")
    let coordinate = mapView.myLocation!.coordinate
    print(coordinate)
    self.draggingCoordinate = marker.position
    // self.placeMarkerOnMapOnButtonTap(coordinate: marker.position)
}

func placeMarkerOnMapOnButtonTap(coordinate : CLLocationCoordinate2D){
    let geocoder = GMSGeocoder()
    geocoder.reverseGeocodeCoordinate(coordinate) {
        response, error in
        //
        if error != nil {
            print("reverse geodcode fail: \(error!.localizedDescription)")
        }
        else {
            if let places = response?.results() {
                if let place = places.first {
                    if let lines = place.lines {
                        print("GEOCODE: Formatted Address: \(lines)")
                        let temp = CoordinatesValue(lineString: lines.first ?? "", title: "", lat: coordinate.latitude, long: coordinate.longitude, isSelect: false, isCompleted: false)
                        self.locArray.append(temp)
                    }
                    if let newPlace = place.postalCode{
                        print(newPlace)
                    }
                }
                else {
                    print("GEOCODE: nil first in places")
                }
            }
            else {
                print("GEOCODE: nil in places")
            }
        }
    }

    let position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
    let  marker = GMSMarker(position: position)
    marker.title = ""
    marker.map = self.googleMapView
    self.selectedCoordinate = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
    //    let distanceInMeters : CLLocationDistance = self.currentCoordinate.distance(from: selectedCoordinate)
    //   print(distanceInMeters)
    print("The CURRENT LAT \(lat) & LONG \(long)")
}

欢迎来到 Stack Overflow。在这里回答问题是很好的,但如果回答能够适当地格式化,并且解释清楚代码的作用以及如何解决问题,那么它将会更加有用。 - AdrianHHH

0
这是一个简单的方法,将Map和其上的项放置在上面。 创建您的按钮及您想要的任何其他内容UIViewController 如果您在ViewController上有任何覆盖默认UIView的UIView,请将它们的背景颜色设置为Clear Color,然后在viewDidLoad()中添加以下代码。
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 17.0)
    let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
    mapView.isMyLocationEnabled = true
    self.view.insertSubview(mapView, at: 0)

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
    marker.title = "Main Title"
    marker.snippet = "Deescription"
    marker.map = mapView

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接