如何在 iPhone 地图上显示用户的位置和其他点?

发布于 2024-08-27 07:24:15 字数 298 浏览 9 评论 0原文

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

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

Basically I want to show the users location plus a list of selected location on a map. It can even have the standard iphone annotations. But, I have no idea of the general steps I would take to achieve this. Would I use MKMapView, or Core Location, or both? Could someone give me a simple outline of steps to take, or a link to a good tutorial or sample code. Thanks

To expand, I was wondering if there are any examples anywhere on how to deal with arrays of locations. I'm guessing that I would need to identify the users location then set up a radius of how far I want to reference locations away from the user, then populate that radius with an array of location that fit within that radius. Are my thoughts on this correct? And are there any examples out there of how to do at least a part of this. I have seen a ton of examples on how to show a single location, but none dealing with multiple locations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自唱情﹋歌 2024-09-03 07:24:15

这是我正在使用的东西,可能会对您有所帮助。它将为您提供一个适合 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 足够大以容纳它们。上面的方法将帮助您做到这一点。但是,如果您想获取适合给定地图区域的位置列表,那么您必须或多或少地执行与我上面所做的相反的操作。

Here's something I'm using that may help you. It will give you an MKCoordinateRegion that fits an array of CLLocations. You can then use that region to pass it to 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);
}

As you're probably already established, you'll need to use a MKMapView and Core Location to do what you want. In my app I know what locations I want to display, and then make the MKMapView big enough to fit them all in. The method above will help you do that. If however you want to get a list of locations that fit within a given map region, then you'll have to do more or less the reverse of what I'm doing above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文