ios - 如何显示设定距离内的注解(Swift 4.0)

标签 ios swift mkmapview

我试图在注释上显示 1 公里以内的所有公交车站,但是,UIMap 显示的注释不在 1 公里以内。当我改为 10km 时,它会显示 1km 以外的注释,但不够近。

类值:可编码{ let busStopCode, roadName, description: 字符串 让纬度,经度:Double

enum CodingKeys: String, CodingKey {
    case busStopCode = "BusStopCode"
    case roadName = "RoadName"
    case description = "Description"
    case latitude = "Latitude"
    case longitude = "Longitude"
}

init(busStopCode: String, roadName: String, description: String, latitude: Double, longitude: Double) {
    self.busStopCode = busStopCode
    self.roadName = roadName
    self.description = description
    self.latitude = latitude
    self.longitude = longitude
}
func GetDistance(latitude: Double, longitude: Double) -> Double {
    let selectedCoordinate = CLLocation(latitude: latitude, longitude: longitude)
    let busStopCoordinate = CLLocation(latitude: Double(self.latitude), longitude: self.longitude)

    return busStopCoordinate.distance(from: selectedCoordinate)
}

View Controller 类

@IBAction func GPSTrack(_ sender: Any) {
    InputAllAnnotation(busStops: stopSearchResults)
    print("Searching for nearby bus stops")
}

func InputAllAnnotation(busStops: [Value]) {
    for busStop in busStops{
        let busStopObj = BusStopAnnotation(value: busStop)
        Annotations.append(busStopObj)

        let distance = busStop.GetDistance(latitude: Double(currentLocation?.latitude ?? 0), longitude: Double(currentLocation?.longitude ?? 0))
        if  distance < 10000 {
            mapView.addAnnotation(busStopObj)
        }
    }
}

我在 10 公里外找到了公共(public)汽车站,但在 1 公里内没有 Image of annotations on Map

最佳答案

检查与 busStops 和你的距离(或任何值)

//for 1km
let distance = myLocation.distance(from: busStopLocation) / 1000

然后检查巴士站

if  distance <= 0.1{

self.displayAlert(title: "INFO", message: "BUS STOP NEAR 1KM")
}

检查距离 busStop 10km

let distance = myLocation.distance(from: busStopLocation) / 10000

如果距离 > 0.1{

self.displayAlert(title: "INFO", message: "BUS STOP AT 10KM")
}

关于ios - 如何显示设定距离内的注解(Swift 4.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359888/

相关文章:

ios - 曼哈顿启发式 IDA* 算法的正确实现是什么?

ios - 调用 API 时不显示 UICollectionView 单元格

ios - 自定义 Apple map 注释无法正常工作

ios - 长按识别器结束后调用 wkwebview decidePolicyForNavigationAction

带有三元运算符的 Swift UITableView numberOfRowsInSection

ios - gpus_ReturnNotPermittedKillClient 发生大量崩溃

iOS 8 自定义键盘

ios - 如何确保在测验应用程序中显示正确的答案选择?

ios - 反转 MKPolygon(由内而外)

ios - map 注释添加图像