我正在尝试构建一个应用程序,在其中可以通过按按钮将标记添加到地图。我已经可以通过按下按钮来实现,将当前位置和默认标题以类型[[MGLPointAnnotation写入数组。但是我对新注释的visualization感到困惑。我总是收到错误
Type of expression is ambiguous without more context
到目前为止是我的代码:
func makeEntry(){
guard let locValue: CLLocationCoordinate2D = locationManager.location?.coordinate else { return }
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
let point = Annotation()
point.coordinate = CLLocationCoordinate2D(latitude: (locValue.latitude), longitude: (locValue.longitude))
point.title = "Entry"
pointAnnotations.append(point)
我是Swift的新手,希望您能帮助我。
}
。无论哪种方式,都令人困惑。此外,guard
语句似乎在使用locationManager.startUpdatingLocation()
之前检查您是否具有有效的位置,这似乎很奇怪。同样,一旦您启动locationManager,您就应该真正使用委托函数locationManager:didUpdateLocations
来获取位置更新,而不是使用locationManager.location
来获取最新,最准确的值。
无论您选择做什么,从启动locationManager到开始接收位置更新之间的时间都是未知的;通常不会很长,但都是未知的。这意味着您不应该只是开始就期望可以立即使用该位置。这就是为什么使用委托功能更好的原因,因为当gps / wifi /无线电接收器开始向您提供值时,它会提醒您。
什么是Annotation
?以下代码将在地图上显示注释。
func addAnnotation() {
let annotation = MGLPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 51.1789, longitude: -1.8262)
annotation.title = "Stonehenge"
annotation.subtitle = "Old stones"
mapView.addAnnotation(annotation)
// Center the map on the annotation.
mapView.setCenter(annotation.coordinate, zoomLevel: 17, animated: false)
}
该位置是硬编码的。如果您在将其适配到locationManager时遇到问题,则应打开一个单独的问题。希望这会有所帮助。