gpt4 book ai didi

swift - 在 MKMapView 的区域成功设置为用户当前位置后,从 api 加载数据并显示注释

转载 作者:行者123 更新时间:2023-11-30 12:16:46 26 4
gpt4 key购买 nike

我有一个 MKMapView,我在其中显示注释,并且位置信息来自 API。我添加了一个按钮,它将触发从 API 获取数据并在 map View 中显示相应的注释。但是,我正在尝试实现这样的事情:

用户登陆 map View 场景,当前位置会更新,并且 map View 区域会设置为用户位置周围的特定边界。 map View 的 .setRegion(_:animated:) 动画完成后,将向用户显示一些注释,而无需用户专门点击显示注释的按钮。

但是在经历了 CLLocationManagerDelegateMKMapViewDelegate 的不同委托(delegate)方法之后,我找不到任何有用的解决方案来满足我的要求。有时,注释是在区域设置动画完成之前添加的。有时它们是在动画完成后添加的。

我使用CLLocationManger来获取用户的当前位置。通过 locationManager(_:didUpdateLocations:) 委托(delegate)方法,我正在设置 map View 的区域:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location.coordinate, span: span)
mapView.setRegion(region, animated: true)
mapView.showsUserLocation = true

// Remove activity indicator
effectView.removeFromSuperview()

// This method is responsible for getting location information from the api and adding annotations to the map view
findPlaces(withType: "restaurant")
}

在上面的委托(delegate)方法中,我调用 findPlaces(withType:) 方法,该方法将从 API 获取数据并向 map 添加注释。 API 需要一个位置和半径来发送回数据。

在我的 mapView(_:regionDidChangeAnimated:) 委托(delegate)中,我正在跟踪 map View 的中心位置和可见区域,稍后当用户点击 在此处显示按钮。

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

let visibleMapRect = mapView.visibleMapRect
let leftMapPoint = MKMapPointMake(MKMapRectGetMinX(visibleMapRect), MKMapRectGetMidY(visibleMapRect))
let rightMapPoint = MKMapPointMake(MKMapRectGetMaxX(visibleMapRect), MKMapRectGetMidY(visibleMapRect))

mapViewDistance = MKMetersBetweenMapPoints(leftMapPoint, rightMapPoint)
mapViewCenter = mapView.centerCoordinate
}

How to show annotations to the user automatically after map view's center is set to the user's current location and the viewable region of the map view is done setting with animation?

这是几乎预期的输出:

这不是我想要的输出:

编辑:

以下是 findPlaces(withType:) 的实现方式:

func findPlaces(withType: String?) {

// preparing an url from the passed string here
queryURL = ...

// get the json data asynchronously
DataManager.loadData(from: queryURL) { (data, error) in

if let error = error {
// Handled error case
} else {
do {
guard let data = data else {
// throw error
}
let json = try JSONSerialization.jsonObject(with: data, options: [])
guard let placesDictionary = json as? JSON else {
// throw error
}
guard let places = Places(json: placesDictionary) else {
// throw error
}
guard let results = places.results else {
// throw error
}

// data is fed into results array, now plot this into the map view
// UI Update should be on the main thread
DispatchQueue.main.async {
self.plotPlaces(with: results)
}
} catch {
print(error)
}
}
}
}

最佳答案

尝试在func mapView(MKMapView,regionDidChangeAnimated: Bool)方法中获取位置信息并设置注释

func mapView(MKMapView, regionDidChangeAnimated: Bool) {

// This method is responsible for getting location information from the api and adding annotations to the map view.
findPlaces(withType: "restaurant")

}

关于swift - 在 MKMapView 的区域成功设置为用户当前位置后,从 api 加载数据并显示注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45341696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com