动画地图到位置不工作谷歌地图 iOS
Posted
技术标签:
【中文标题】动画地图到位置不工作谷歌地图 iOS【英文标题】:Animate Map to Location is Not Working Google Maps iOS 【发布时间】:2017-10-12 15:37:22 【问题描述】: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)
我设置了以下代码,旨在在谷歌地图上放置一个按钮,当点击该按钮时,谷歌地图相机将动画到该位置。但是,我的代码不起作用。 “轻拍”在控制台中打印,但相机没有让步。我无法在任何地方找到答案,因此我们将不胜感激。
编辑:添加了地图视图控制器的完整代码
【问题讨论】:
您是否明确调用按钮操作? @Nirav D 是的,它正在被调用。每次点击按钮时都会打印“已点击”。 在print(Tapped)
和另一个打印语句之后,如print(place.latitude, place.longitude)
并在此处添加它的控制台日志
窃听 34.0522 -118.2437
问题出在这一行view = mapView
of viewDidLoad
用self.mapView = mapView
更改我还编辑了我的答案
【参考方案1】:
在我的情况下,地图没有更新,因为我没有调用主队列上的方法。下面的代码解决了这个问题:
DispatchQueue.main.async
self.mapView.animate(to: camera)
任何与用户界面相关的操作都应该在主队列中调用
【讨论】:
这对我帮助很大。谢谢!【参考方案2】:试试这个方法
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)
【讨论】:
仍然没有动画。在这一点上,我认为这一定是我的代码中其他地方的错误,因为没有任何作用。 据我所知,没有异步调用。它只是一个 MapView 代码,它从火力库中获取引脚的数据并将它们显示在地图上。我认为 Firebase 加载不是问题。 我可以发布整个地图视图代码,如果这样更有帮助吗? 地方存在——我查过了。我在上面添加了完整的代码。 我试过了,但我得到了一个空白屏幕。我应该将var mapView = GMSMapView()
更改为var mapView: GMSMapView!
吗?以上是关于动画地图到位置不工作谷歌地图 iOS的主要内容,如果未能解决你的问题,请参考以下文章