ios - 动画 map 到位置不工作谷歌地图 iOS

标签 ios swift google-maps button gmsmapview

import UIKit
import GoogleMaps
import FirebaseDatabase
import GeoFire

class MapViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
    var mapView = GMSMapView()

    var locationManager: CLLocationManager!
    let regionRadius: CLLocationDistance = 1000
    var place = CLLocationCoordinate2D()

    @IBOutlet var myLocationButton: UIButton!
    @IBOutlet var infoWindow: UIView!
    @IBOutlet var postTitle: UILabel!
    @IBOutlet var postImage: UIImageView!

    var showing = false;
    var pins = [String: Pin]()
    var currentMarker = GMSMarker()

    override func viewDidLoad() {
        super.viewDidLoad()

        // sets up the map view (camera, location tracker etc.)
        let camera = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 17.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        mapView.isMyLocationEnabled = true
        mapView.delegate = self
        view = mapView

        self.view.addSubview(myLocationButton)
        self.view.bringSubview(toFront: myLocationButton)

        // Location manager
        locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        locationManager.requestAlwaysAuthorization()
        locationManager.delegate = self
        locationManager.startUpdatingLocation()

        // Get nearby records
        let geoFire = GeoFire(firebaseRef: FIRDatabase.database().reference().child("geofire"))
        let query = geoFire?.query(at: CLLocation(latitude: place.latitude, longitude: place.longitude), withRadius: 0.6)

        _ = query?.observe(.keyEntered, with: { (key, location) in
            let marker = GMSMarker()
            let newPin = Pin(title: "post", locationName: "\(key!)", discipline: "", coordinate: (location?.coordinate)!)
            self.pins[newPin.locationName] = newPin
            marker.icon = UIImage(named: "icon_small_shadow")
            marker.position = Pin.coordinate
            marker.title = Pin.title
            marker.snippet = Pin.locationName
            marker.map = mapView
        })

        myLocationTapped(myLocationButton)

    }

    // sets the info in the custom info window
    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {

         if(currentMarker == marker && showing) {
           infoWindow.isHidden = true
            showing = false
         } else {
            infoWindow.isHidden = false
            self.view.addSubview(infoWindow)
            self.view.bringSubview(toFront: infoWindow)
            postTitle.text = marker.snippet
            showing = true
        }

        currentMarker = marker

        return true
    }

    @IBAction func myLocationTapped(_ sender: Any) {
       print("tapped")
       let cameraPosition = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 15.0)
       mapView.animate(to: cameraPosition)
    }

我设置了以下代码,旨在在谷歌地图上放置一个按钮,当点击该按钮时,谷歌地图相机会动画到该位置。但是,我的代码不起作用。 “点击”打印在控制台中,但相机没有移动。我无法在任何地方找到答案,所以任何帮助将不胜感激。

编辑:为 map View Controller 添加了完整代码

最佳答案

在我的例子中, map 没有更新,因为我没有在主队列上调用方法。下面的代码解决了这个问题:

DispatchQueue.main.async {
        self.mapView.animate(to: camera)
    }

任何与用户界面相关的 Action 都应该在主队列中调用

关于ios - 动画 map 到位置不工作谷歌地图 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43952595/

相关文章:

ios - initWithNibName 没有被调用

ios - iOS 应用的照片尺寸和分辨率

ios - 在 UITableView 中将 x 位置设置为 UITablecell

ios - 5分钟后如何在后台调用api?

javascript - 带有 ngmap Angularjs 的 html 中的客户标记

ios - iOS 中哪个更重要 : Memory or CPU?

ios - NavigationBar 后退按钮在 popViewController 之后更改文本

swift - 在 Swift 中创建线程安全读取时,为什么在并发队列之外创建一个变量?

ios - 如何在 Google Map iOS Swift 中获取点击位置的经纬度

javascript - 事件监听器将结果相乘