【问题标题】:How to fetch user current location and some other coordinate are same or not?如何获取用户当前位置和其他坐标是否相同?
【发布时间】:2016-08-01 08:05:29
【问题描述】:

我想将用户当前位置与其他位置进行匹配,无论它们是否相同。

我的逻辑:

            point.coordinate = coordinate
            let userLat = mapView.userLocation?.coordinate.latitude
            let userLong = mapView.userLocation?.coordinate.longitude
            if point.coordinate.latitude == userLat && point.coordinate.longitude == userLong
            {
            point.title = "You are here"

            }
            else
            {
            point.title = "\(coordinate.latitude), \(coordinate.longitude)"
            }

但问题是,mapView.userLocation?.coordinate.latitude 不等于 point.coordinate.latitude。但是我在模拟器和其他坐标中为 userLocation 设置了相同的值。

我为用户位置设置的值是: 纬度:28.6 长:77.35

我为其他坐标设置的值是: 纬度:28.6 长:77.35

我从用户位置获得的价值是: 纬度:可选 - 一些:3.4028234663852886e+38

长:▿ 可选 - 一些:3.4028234663852886e+38

我得到的其他坐标值是: 纬度:28.6 长:77.35

如果有更好的逻辑来找出这件事,那么请告诉我。

【问题讨论】:

    标签: ios google-maps maps mapkit mapbox


    【解决方案1】:

    您的点对象是 CLLocation 子类的对象吗?如果没有,请从您的 point.coordinate 创建 CLLocation 并进行如下比较。 试试这个比较 CLLocation 对象,而不是坐标,使用这种方法:

    let distanceThreshold: CLLocationDistance = 2.0 // in meters
    if point.distanceFromLocation(mapView.userLocation!) < distanceThreshold {
    point.title = "You are here"
    } else {
    point.title = "\(coordinate.latitude), \(coordinate.longitude)"
    }
    

    或者您可以将阈值设置为 0

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 2017-07-07
      相关资源
      最近更新 更多