作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用 Google Maps iOS SDK 在 map 上显示用户的位置?
我试图在 self.myMapView(MKMapView)
中找到 .myLocation
属性,但找不到。
任何人都可以逐步向我解释如何在 map 上显示我的用户位置吗?
最佳答案
像这样为 GMSMapView 和 CLLocationManager 创建属性
@IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
var didFindMyLocation = false
并在 viewdidload 方法中为 map View 添加观察者以进行位置更新,如下所示
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(48.857165, longitude: 2.354613, zoom: 2.0)
mapView.camera = camera
mapView.delegate = self
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
//标记:- CLLocationManagerDelegate
extension MapViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
mapView.myLocationEnabled = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
didFindMyLocation 是 bool 属性,也不要忘记将 'NSLocationWhenInUseUsageDescription' 键添加到 Info.plist 文件中
关于ios - 找不到谷歌地图 mapview.myLocation (iOS SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33936598/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!