gpt4 book ai didi

ios - 尝试选择尚未添加的注释

转载 作者:可可西里 更新时间:2023-11-01 02:18:41 24 4
gpt4 key购买 nike

我正在使用 swift 和 mapkit 开发一个应用程序。除了 mainUser 注释 View 之外,mapView 还有一些注释。我想要的是当用户从另一个 ViewController 选择 UITableViewCell 中的一个单元格时,他应该被带到 mapView ViewController 并选择一个特定的引脚。我通过传递一个名为“fromSegue”的对象来做到这一点。所有这一切都很好,除非用户想要选择自己:选择任何类型的 CustomPointAnnotationView 都没有问题,但在选择 userLocation 时,尚未调用 ViewForAnnotation 函数。

这里是设置 map 区域的函数。这是应该进行注释选择的地方。

    func setFPS() {
print("[SFPS] - setFPS")
self.mainUser.setObject(mainUserLocation.latitude, forKey: "latitude")
self.mainUser.setObject(mainUserLocation.longitude, forKey: "longitude")
if self.fromSegue != nil {
print("[SFPS] - setModFPS")
if fromSegue.type == 11 {
print("[SFPS] - type11 - Showing a notif of type 11")
let latititi:Double = fromSegue.latitude as! Double
let longigigi:Double = fromSegue.longitude as! Double
self.mapScope = CLLocationCoordinate2DMake(latititi, longigigi)
var annotJar = pinJar.filter{$0.nickName == self.fromSegue.name}
print("[SFPS] - type11 - ALL RIGHT LETS DO IT")
let annot = annotJar[0]
Map.selectAnnotation(annot,animated: true)
fromSegue = nil
print("[SFPS] - type11 - Over")
} else if fromSegue.type == 21 {
print("[SFPS] - type21 - Showing a notif of type 21")
Map.selectAnnotation(self.Map.userLocation,animated: true)
self.mapScope = mainUserLocation
fromSegue = nil
} else {
print("[SFPS] - no type! problem here")
self.mapScope = CLLocationCoordinate2DMake(mainUserLocation.latitude, mainUserLocation.longitude)
}
} else {
self.mapScope = CLLocationCoordinate2DMake(mainUserLocation.latitude, mainUserLocation.longitude)
}
Map.rotateEnabled = true
let span = MKCoordinateSpanMake(0.001, 0.001)
print("[SFPS] - span made")
let region = MKCoordinateRegionMake(mapScope, span)
print("[SFPS] - region made")
Map.setRegion(region, animated: true)
print("[SFPS] - region is all set - Over")
}

当fromSegue是类型11时,它应该选择一个customPointAnnotationview。这就是控制台中发生的事情:

[SFPS] - setFPS
[SFPS] - setModFPS
[SFPS] - type11 - Showing a notif of type 11
[SFPS] - type11 - ALL RIGHT LETS DO IT
[VFA] - ViewForAnnotation
[VFA] - the annotation: <PING.CustomPointAnnotation: 0x1890d570>. from: Optional(Optional("newParse44"))
[VFA] - of kind random folk
[VFA] - of kind random folk - building...
[DSAV] - didSelectAnnotationView: Optional(Optional("newParse44"))
[DSAV] - guest
[DSAV] - Optional("newParse44") selected
[SFPS] - type11 - Over
[SFPS] - span made
[SFPS] - region made
[SFPS] - region is all set - Over

正在执行函数setFPS(),就在要选择注释的时候,它被创建,然后被选中,然后setFPS()的其余部分被执行。伟大的!这里一切正常。

现在当 fromSegue 的类型为 21 时,应该选择 userLocation 引脚。这是控制台中发生的事情:

[SFPS] - setFPS
[SFPS] - setModFPS
[SFPS] - type21 - Showing a notif of type 21
2015-11-06 07:40:31.116 PING[1272:574013] ERROR: Trying to select an annotation which has not been added
[SFPS] - span made
[SFPS] - region made
[SFPS] - region is all set - Over

所以在这种情况下,还没有添加 userlocation 注释,因为还没有调用 viewForAnnotation(足够早?)。我的问题是为什么?我可以手动调用 viewForAnnotation 吗?

非常感谢您的帮助

最佳答案

在 MKMapView
selectAnnotation 之前尝试 setCenterCoordinate

例如:

mapview.setCenterCoordinate(mapview.userLocation.coordinate, animated: false)
mapview.selectAnnotation(mapview.userLocation,animated: true)

更新:
获取用户位置需要一些时间。
MKMapViewDelegatedidUpdateUserLocation,它会在每次位置更新时更新。我们想在第一次触发时获取 userLocation 然后选择它。

ViewController 中创建一个 bool var。此 var 将用于首次选择注释。我们想忽略 didUpdateUserLocation 的所有其他回调。

class MapViewController: UIViewController {    
var didUpdateUserLocation = false
...
...
}

现在实现MKMapViewDelegatedidUpdateUserLocation函数。

extension MapViewController : MKMapViewDelegate {

func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {

if !didUpdateUserLocation {
mapView.selectAnnotation(mapView.userLocation, animated: true)
didUpdateUserLocation = true
}
}
}

关于ios - 尝试选择尚未添加的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33560980/

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