简体   繁体   English

动画地图到位置不起作用谷歌地图iOS

[英]Animate Map to Location is Not Working Google Maps iOS

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)
    }

I have the following code set up, designed to place a button on the google maps map that when tapped, animates the google maps camera to that location. 我有以下代码设置,旨在在谷歌地图上放置一个按钮,当点击时,将谷歌地图相机动画到该位置。 However, my code doesn't work. 但是,我的代码不起作用。 The "tapped" prints in the console but the camera doesn't budge. “轻拍”打印在控制台中,但相机不会让步。 I haven't been able to find an answer anywhere for this, so any help would be appreciated. 我无法在任何地方找到答案,所以任何帮助都将不胜感激。

EDIT: Added full code for the Map View Controller 编辑:添加了地图视图控制器的完整代码

In my case the map wasn't updating because I was not calling the method on the main queue. 在我的情况下,地图没有更新,因为我没有在主队列上调用方法。 The following code solved the issue: 以下代码解决了该问题:

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

Anything action related to the user interface should be called in the main queue 应在主队列中调用与用户界面相关的任何操作

Try this way 试试这种方式

let cameraPosition = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 15.0)
mapView.animate(to: cameraPosition)

Edit: Issue is you aren't having the reference of your map with your mapView object, change your viewDidLoad 's line: 编辑:问题是你没有使用mapView对象引用你的地图,改变你的viewDidLoad的行:

view = mapView

TO: 至:

// 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: view.bounds, camera: camera)
mapView.isMyLocationEnabled = true
mapView.delegate = self
self.mapView = mapView
view.addSubview(self.mapView)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM