简体   繁体   English

MapKit注释和用户位置

[英]MapKit Annotations and User location

I followed this tutorial to make my first app: 我按照本教程制作了第一个应用程序:

http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/ http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/

I would really like to know how to sort the annotations in the Table in order of distance to the user (the nearest annotation is the first one on the table) How is it possible to do that? 我真的很想知道如何按与用户的距离排序表中的注释(最近的注释是表中的第一个注释)怎么可能呢?

I understand that I must use the CLLocation to find the user's location but then I have no idea. 我知道我必须使用CLLocation来找到用户的位置,但是我不知道。

Could any one help me? 有人可以帮我吗?

Cheers, 干杯,

Thank you in advance for your much appreciated help, 预先感谢您的帮助,

EDIT: I've added details: 编辑:我添加了详细信息:

the data is not in an array, it is implemented in RootViewController.m in this form: 数据不在数组中,它在RootViewController.m中以以下形式实现:

-(void)loadOurAnnotations
{
    CLLocationCoordinate2D workingCoordinate;

    workingCoordinate.latitude = 40.763856;
    workingCoordinate.longitude = -73.973034;
    iCodeBlogAnnotation *appleStore1 = [[iCodeBlogAnnotation alloc] 

    initWithCoordinate:workingCoordinate];
    [appleStore1 setTitle:@"Apple Store 5th Ave."];
    [appleStore1 setSubtitle:@"Apple's Flagship Store"];
    [appleStore1 setAnnotationType:iCodeBlogAnnotationTypeApple];

    [mapView addAnnotation:appleStore1];

... and so on. ... 等等。 How is it possible to do it then? 那怎么可能呢?

You can find the source code here: 您可以在此处找到源代码:

icodeblog.com/wp-content/uploads/2009/09/iCodeMap.zip icodeblog.com/wp-content/uploads/2009/09/iCodeMap.zip

teddafan teddafan

On a CLLocation you can use CLLocation您可以使用

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

to calculate the distance of an object from another. 计算一个物体到另一个物体的距离。 In this case, it would presumably be the user's current location. 在这种情况下,大概是用户的当前位置。

If the data is in an NSArray you can use sortedArrayUsingFunction to call a helper function that calls this method. 如果数据在NSArray ,则可以使用sortedArrayUsingFunction调用调用此方法的帮助函数。

Loop through the CLLocations and find the distance between them and your current location. 遍历CLLocations并找到它们与您当前位置之间的距离。

Then use your own sorting algorithm to sort the distances you are having in an array. 然后使用自己的排序算法对数组中的距离进行排序。

for (CLLocation *loc in locations) {
  [distances addobject:[currentLocation distanceFromLocation:loc]];
}
[distances sort]; /* Own sorting algorithm */
[yourTable reloadData];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM