【问题标题】:Latitudinal/Longitudinal difference in meters between two coordinates?两个坐标之间的纬度/经度差(以米为单位)?
【发布时间】:2017-03-23 12:19:24
【问题描述】:

我正在尝试创建一个用户位置位于中心的 MKCoordinateRegion,并且在该区域内应该可以看到另一个坐标。简而言之,我试图以用户位置为中心,同时显示一定数量的注释。我的方法是查看第 N:th 最远坐标所在的位置,并尝试从用户位置坐标计算到该坐标的距离或坐标区域。

    var topLeftCoord = CLLocationCoordinate2D(latitude: -90.0, longitude: 180.0)
    var bottomRightCoord = CLLocationCoordinate2D(latitude: 90.0, longitude: -180.0)

    topLeftCoord.longitude = fmin(topLeftCoord.longitude, outerLongitude)
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, outerLatitude)
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, outerLongitude)
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, outerLatitude)

    topLeftCoord.longitude = fmin(topLeftCoord.longitude, userCoordinates.longitude)
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, userCoordinates.latitude)
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, userCoordinates.longitude)
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, userCoordinates.latitude)

    let regionSpan = MKCoordinateSpanMake(fabs((topLeftCoord.latitude - bottomRightCoord.latitude)), fabs((bottomRightCoord.longitude - topLeftCoord.longitude)))
    var region = MKCoordinateRegion(center: userCoordinates, span: regionSpan)

    region = self.mapView.regionThatFits(region)
    self.mapView.setRegion(region, animated: true)

这种工作,但我得到了一点抵消。我认为问题在于这里我没有考虑到用户位置应该在中心,所以这段代码只是确保用户和outerLatitudeouterLongitude 的坐标在范围内。但是当以用户坐标为中心时,外部坐标不再可见。

我这辈子都想不通。我也试过这样做:

    let outerLocation = CLLocation(latitude: outerLatitude, longitude: outerLongitude)
    let userLocation = CLLocation(latitude: userCoordinates.latitude, longitude: userCoordinates.longitude)

    let distance = userLocation.distance(from: outerLocation)
    var region = MKCoordinateRegionMakeWithDistance(userCoordinates, distance, distance)

    region = self.mapView.regionThatFits(region)
    self.mapView.setRegion(region, animated: true)

但我最终得到了相同的结果,我真的不明白。我得到一个大约 340m 的距离(这是相当合理的)。但是MKCoordinateRegionMakeWithDistance 要求latitudinalMeterslongitudinalMeters,我只是将distance 从坐标A 发送到B。我找不到将组件拆分为latitudinalMeterslongitudinalMeters 的方法,我可以简单地得到两者之间的1距离。

【问题讨论】:

    标签: ios swift mapkit


    【解决方案1】:

    我找不到将组件拆分为纬度米和纵向米的方法,我可以简单地得到两者之间的 1 个距离

    尝试创建一个与您的用户位置具有相同纬度和与您的外部位置相同经度的新点,并使用它来获取“纬度距离”

    然后创建一个与用户位置相同的对数和与外部位置相同的纬度的点,并使用它来获取纵向距离。

    注意:如果外部点比用户点更靠近赤道,您可能需要使用两个新点的外部点的纬度来获取纬度距离。

    【讨论】:

    • 结果几乎一样。我一定在这里做错了什么。
    【解决方案2】:

    还有一种方法

    var region = MKCoordinateRegionMake(userLocation!, MKCoordinateSpanMake(4 * abs(destination.latitude - userLocation!.latitude), 4 * abs(destination.longitude - userLocation!.longitude)))
    self.mapView.setRegion(region2, animated: true)
    

    我不得不稍微使用乘数来获得正确的范围 - 但MKCoordinateSpanMake 将适用于纬度/经度差异,您确实可以使用。如果您希望用户位置居中,看起来您至少需要将计算的角度差加倍,我们可能应该做一些事情来找出纬度/经度中的哪个更大并将其用于两者。

    这应该会让你朝着正确的方向前进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2016-01-22
      • 2011-09-16
      • 1970-01-01
      • 2018-01-10
      • 2011-09-05
      相关资源
      最近更新 更多