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

我有以下代碼設置,旨在在谷歌地圖上放置一個按鈕,當點擊時,將谷歌地圖相機動畫到該位置。 但是,我的代碼不起作用。 “輕拍”打印在控制台中,但相機不會讓步。 我無法在任何地方找到答案,所以任何幫助都將不勝感激。

編輯:添加了地圖視圖控制器的完整代碼

在我的情況下,地圖沒有更新,因為我沒有在主隊列上調用方法。 以下代碼解決了該問題:

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

應在主隊列中調用與用戶界面相關的任何操作

試試這種方式

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

編輯:問題是你沒有使用mapView對象引用你的地圖,改變你的viewDidLoad的行:

view = mapView

至:

// 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