通过iOS上Mapbox中的按钮在当前位置添加Maker

问题描述 投票:0回答:1

我正在尝试构建一个应用程序,在其中可以通过按按钮将标记添加到地图。我已经可以通过按下按钮来实现,将当前位置和默认标题以类型[[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的新手,希望您能帮助我。     
ios dictionary annotations location mapbox
1个回答
0
投票
您的代码似乎很混乱,但是由于您提供的代码很少,因此很难确切说明问题出在哪里/什么地方。您显示的功能不完整或至少缺少最后的结算}。无论哪种方式,都令人困惑。

此外,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时遇到问题,则应打开一个单独的问题。希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.