【问题标题】:How do I show the user's location, and additional points on an iPhone map?如何在 iPhone 地图上显示用户的位置和其他点?
【发布时间】:2011-02-01 23:26:37
【问题描述】:

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

为了扩展,我想知道是否有任何关于如何处理位置数组的示例。我猜我需要确定用户的位置,然后设置一个半径,说明我想引用离用户多远的位置,然后用适合该半径的位置数组填充该半径。我对此的想法正确吗?是否有任何示例说明如何至少完成其中的一部分。我已经看到了大量关于如何显示单个位置的示例,但没有一个涉及多个位置。

【问题讨论】:

    标签: iphone mkmapview core-location


    【解决方案1】:

    这是我正在使用的东西,可能会对您有所帮助。它将为您提供适合 CLLocations 数组的 MKCoordinateRegion。然后,您可以使用该区域将其传递给 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 足够大以适应它们。上面的方法将帮助您做到这一点。但是,如果您想获得适合给定地图区域的位置列表,那么您将不得不或多或少地与我上面所做的相反。

    【讨论】:

    • 这也适用于 Apple Watch 的 WKInterfaceMap 中的“缩放以适应”=)。
    【解决方案2】:

    【讨论】:

    • 感谢您的帮助,但经过几次谷歌搜索后,我已经看到了这些链接。这些教程相当简单,仅适用于一个位置。有什么更高级的吗?
    猜你喜欢
    • 2018-04-25
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多