iphone - 如何在 iPhone map 上显示用户的位置和其他点?

标签 iphone mkmapview core-location

基本上我想在 map 上显示用户位置以及所选位置的列表。它甚至可以有标准的 iPhone 注释。但是,我不知道实现这一目标将采取的一般步骤。我会使用 MKMapView 或 Core Location,还是两者都使用?有人可以给我一个简单的步骤概要,或者一个好的教程或示例代码的链接。谢谢

为了扩展,我想知道是否有任何关于如何处理位置数组的示例。我猜测我需要识别用户位置,然后设置我想要引用距用户多远的半径,然后用适合该半径的位置数组填充该半径。我对此的想法正确吗?有没有任何例子可以说明如何至少完成其中的一部分。我见过很多关于如何显示单个位置的示例,但没有一个涉及多个位置。

最佳答案

这是我正在使用的东西,可能会对您有所帮助。它将为您提供一个适合 CLLocations 数组的 MKCooperativeRegion。然后,您可以使用该区域将其传递给 MKMapView setRegion:animated:

// create a region that fill fit all the locations in it
+ (MKCoordinateRegion) getRegionThatFitsLocations:(NSArray *)locations {
    // initialize to minimums, maximums
    CLLocationDegrees minLatitude = 90;
    CLLocationDegrees maxLatitude = -90;
    CLLocationDegrees minLongitude = 180;
    CLLocationDegrees maxLongitude = -180;

    // establish the min and max latitude and longitude
    // of all the locations in the array
    for (CLLocation *location in locations) {
        if (location.coordinate.latitude < minLatitude) {
            minLatitude = location.coordinate.latitude;
        }
        if (location.coordinate.latitude > maxLatitude) {
            maxLatitude = location.coordinate.latitude;
        }
        if (location.coordinate.longitude < minLongitude) {
            minLongitude = location.coordinate.longitude;
        }
        if (location.coordinate.longitude > maxLongitude) {
            maxLongitude = location.coordinate.longitude;
        }
    }

    MKCoordinateSpan span;
    CLLocationCoordinate2D center;
    if ([locations count] > 1) {
        // for more than one location, the span is the diff between
        // min and max latitude and longitude
        span =  MKCoordinateSpanMake(maxLatitude - minLatitude, maxLongitude - minLongitude);
        // and the center is the min + the span (width) / 2
        center.latitude = minLatitude + span.latitudeDelta / 2;
        center.longitude = minLongitude + span.longitudeDelta / 2;
    } else {
        // for a single location make a fixed size span (pretty close in zoom)
        span =  MKCoordinateSpanMake(0.01, 0.01);
        // and the center equal to the coords of the single point
        // which will be the coords of the min (or max) coords 
        center.latitude = minLatitude;
        center.longitude = minLongitude;
    }

    // create a region from the center and span
    return MKCoordinateRegionMake(center, span);
}

由于您可能已经建立,因此您需要使用 MKMapView 和核心位置来执行您想要的操作。在我的应用程序中,我知道要显示哪些位置,然后使 MKMapView 足够大以容纳它们。上面的方法将帮助您做到这一点。但是,如果您想获取适合给定 map 区域的位置列表,那么您必须或多或少地执行与我上面所做的相反的操作。

关于iphone - 如何在 iPhone map 上显示用户的位置和其他点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2536939/

相关文章:

iphone - 找不到此可执行文件的有效配置文件

ios - 用户定位引脚的自定义注释

ios - 当手指沿着 View 移动时,如何跟踪 UIView 中触摸输入的 CGPoint?

ios - MKAnnotationView 未从 MKMapView 中删除

ios - viewDidLoad时获取用户的经度/纬度

iOS 3 和 iOS 4 核心位置权限对话框

iPhone 5和 Storyboard设置自动调整大小蒙版

javascript - 输入焦点上的 iOS 键盘部分触发

c# - 为什么我在 UITableView 中有一个空的(但有效的)表索引带?

ios - 哪个应用程序授权对话框最先显示?推送或定位