我有一系列词典。每个字典都包含纬度和经度,因此我从当前用户位置获取每个项目的距离。如果以英里为单位的距离大于20,则应从阵列中删除该特定字典。如果未从数组中删除字典,则会创建注释并将其添加到注释数组,然后在枚举完成后将其用于向地图添加注释。当我应该得到三个时,我只会添加一个注释,所以我知道我在枚举时做错了。
func checkDistanceAndAddPins() {
for gym in gyms {
var index = 0
let gymLatitude = gym["latitude"]!!.doubleValue
let gymLongitude = gym["longitude"]!!.doubleValue
let gymLocation = CLLocation(latitude: gymLatitude, longitude: gymLongitude)
let distance = gymLocation.distanceFromLocation(myLocation!)
let distanceInMeters = NSNumber(double: distance)
let metersDouble = distanceInMeters.doubleValue
let miles = metersDouble * 0.00062137
if miles > maxDistance {
gyms.removeAtIndex(index)
} else {
let location = CLLocationCoordinate2D(latitude: gymLatitude, longitude: gymLongitude)
gymAnnotation.title = gym["Name"] as? String
gymAnnotation.subtitle = gym["Address"] as? String
gymAnnotation.coordinate = location
gymAnnotation.gymPhoneNumber = gym["Phone"] as? String
if let website = gym["Website"] as? String {
gymAnnotation.gymWebsite = website
}
gymLocations.append(gymAnnotation)
}
index += 1
}
dispatch_async(dispatch_get_main_queue()) {
self.gymMap.addAnnotations(self.gymLocations)
}
}