【发布时间】:2018-01-16 15:10:04
【问题描述】:
import UIKit
import CoreLocation
import GoogleMaps
class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, CLLocationManagerDelegate {
@IBOutlet weak var containView: UIView!
let locManager=CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locManager.delegate = self
self.locManager.desiredAccuracy = kCLLocationAccuracyBest
self.locManager.requestWhenInUseAuthorization()
self.locManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locManager.stopUpdatingLocation()
}
func showCurrentLocationOnMap() {
let camera = GMSCameraPosition.camera(withLatitude: (self.locManager.location?.coordinate.latitude)!, longitude: (self.locManager.location?.coordinate.latitude)!, zoom: 140)
let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.isMyLocationEnabled = true
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Current location"
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = mapView
self.containView.addSubview(mapView)
}
我在我的控制器中添加了这段代码,因为我试图在我当前的位置上显示带有标记的地图(谷歌地图)但没有工作(地图没有显示),我在我的代码中做错了什么?我该如何解决?
【问题讨论】:
-
是否正在调用
locationManager函数?你在模拟器上测试吗? -
我正在设备上测试
-
locationManager被调用了吗?例如在函数上添加断点或更改函数以接受纬度和经度并从 viewdidload 调用它 -
FTF,当你第一次运行你的应用程序时,它肯定不会正常运行。您依次拥有
self.locManager.requestWhenInUseAuthorization()和self.locManager.startUpdatingLocation()。但是self.locManager.requestWhenInUseAuthorization()是一个异步函数,所以第一次用户还没有授权定位,所以self.locManager.startUpdatingLocation()不会执行。关于你的地图,我在你的代码上看不到任何地图参考,你是在 IB 上插入的吗? -
@GIJOW 是的,也许这就是问题所在,我该如何修复它以执行 self.locManager.startUpdatingLocation() ?现在关于您的问题,我的代码就像在克劳迪奥的答案上一样,但它仍然不起作用
标签: ios swift google-maps swift4