【问题标题】:Contain All Coordinates On Screen包含屏幕上的所有坐标
【发布时间】:2011-11-07 15:18:16
【问题描述】:

我使用MKMapOverlayCoreLocation 绘制了许多坐标,并且我希望能够将它们全部包含在视图中。

我怎样才能将它们全部包含在内?我想过在我的第一个坐标和最后一个坐标之间取中间点,但如果不是直接路线,或者如果它们最终位于同一位置,那么某些坐标可能会从屏幕上消失。

例如,这将是一个在右下角剪掉一些点的示例。 注意:这只是为了举例,路径是使用谷歌地图创建的,但在应用程序中,它将通过跟踪您的位置历史来创建。

同样,如果这是在 iPhone 上,我如何使用 MKCoordinateRegion 使地图包含所有点?

【问题讨论】:

标签: iphone objective-c mkmapview core-location


【解决方案1】:

我认为您需要遍历所有坐标并找到北、东、南和西边界。然后可以根据所有值创建一个坐标区域。

也许您可以查看this question 以获得实施帮助?

【讨论】:

    【解决方案2】:

    尝试遍历您的坐标并获取每个纬度和经度的最极端点(最北、最南、最东、最西),然后取极端值的平均值。然后创建一个区域并设置它。

    类似

    for(int idx = 0; idx < points.count; idx++){
        CLLocation* currentLocation = [points objectAtIndex:idx];
        if(currentLocation.coordinate.latitude != -180 && currentLocation.coordinate.longitude != -180){
            if(currentLocation.coordinate.latitude > maxLat)
                maxLat = currentLocation.coordinate.latitude;
            if(currentLocation.coordinate.latitude < minLat)
                minLat = currentLocation.coordinate.latitude;
            if(currentLocation.coordinate.longitude > maxLon)
                maxLon = currentLocation.coordinate.longitude;
            if(currentLocation.coordinate.longitude < minLon)
                minLon = currentLocation.coordinate.longitude;
        }
    }
    
    MKCoordinateRegion region;
    region.center.latitude = (maxLat + minLat) / 2;
    region.center.longitude = (maxLon + minLon) / 2;
    region.span.latitudeDelta = (maxLat - minLat) + .3;
    region.span.longitudeDelta = (maxLon - minLon) + .3;
    
    [map regionThatFits:region];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多