IOS中距离当前位置最近的位置

时间:2023-02-05 20:16:50

Have a button on a mapview which is used to calculate the nearest location from the locations i have plotted on map. I have applied some code but when i click the button the app crashes by giving this error,

在mapview上有一个按钮,用于计算离我在地图上标出的位置最近的位置。我已经应用了一些代码但是当我点击按钮时应用程序会崩溃,

[__NSDictionaryI coordinate]: unrecognized selector sent to instance 0x174e61b40 2017-10-31 15:20:00.434548 GuardsAutoZone[735:114166] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI coordinate]: unrecognized selector sent to instance 0x174e61b40' * First throw call stack: (0x1937d11b8 0x19220855c 0x1937d8268 0x1937d5270 0x1936ce80c 0x10011cec8 0x1996bbd30 0x1996bbcb0 0x1996a6128 0x1996bb59c 0x1996bb0c4 0x1996b6328 0x199686da0 0x199e7075c 0x199e6a130 0x19377eb5c 0x19377e4a4 0x19377c0a4 0x1936aa2b8 0x19515e198 0x1996f17fc 0x1996ec534 0x1000fc994 0x19268d5b8) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

[__NSDictionaryI坐标]:发送给实例0x174e61b40的未识别选择器2017-10-31 15:20:00.434548 GuardsAutoZone[735:114166](1937d11b8 0x19220855c 0x1937d8268 0x1937d5270 0x1937d5270 0x1936ce80c 0x10011cec8 0x1996bb30 0x1996bbcb0 0x19960x1960x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x190x196bb5bb5bb5bb50x190x190x190x190x190x190x190x190x190x190x190x0c50x190x0c50x190x190x193c50x0c50x190x190x190x190x190x190x193c50x140c50x194 0x193c50x194 0x194 0x9c50x194 0x194 0x194 0x194 0x194 0x193c4 0x193c4 0x194 0x9c4 0x194 0x9c4 0x194 0x9c4dylib:使用NSException (lldb)类型的未捕获异常终止。

My code is this, i'm in doubt that my code is right for finding nearest location or not? The code i used is,

我的代码是这样的,我怀疑我的代码是否适合找到最近的位置?我使用的代码是,

- (IBAction)nearLocation:(id)sender {
CLLocation *userLocation = self.mapView.userLocation.location;
NSMutableDictionary *distances = [NSMutableDictionary dictionary];

for (MapPin *obj in locations) {
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:obj.coordinate.latitude longitude:obj.coordinate.longitude];
    CLLocationDistance distance = [loc distanceFromLocation:userLocation];

    NSLog(@"Distance from Annotations - %f", distance);

    [distances setObject:obj forKey:@( distance )];


}

NSArray *sortedKeys = [distances allKeys];
NSLog(@"List %@",sortedKeys);
//NSArray *closestKeys = [sortedKeys subarrayWithRange:NSMakeRange(0, MIN(3, sortedKeys.count))];

 // NSArray *closestAnnotations = [distances objectsForKeys:closestKeys notFoundMarker:[NSNull null]];


}

the error screen shot is , enter image description here

错误的屏幕截图是,在这里输入图像描述

2 个解决方案

#1


0  

Use this set of code to get the distance between two latitude and longitude.

使用这组代码来获取纬度和经度之间的距离。

In Objective C

在Objective - C

@class CLLocation

CLLocation *location1 =[[CLLocation alloc] initWithLatitude:[@"23.039698" doubleValue] longitude:[@"77.571663" doubleValue]];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:[@"23.0422" doubleValue] longitude:[@"72.5644" doubleValue]];
CLLocationDistance distanceMeter = [location distanceFromLocation:location2];

And in Swift

和迅速

import CoreLocation

let location1 = CLLocation(latitude: 5.0, longitude: 5.0)
let location2 = CLLocation(latitude: 5.0, longitude: 3.0)

let distanceInMeters = location1.distance(from: location2)

The above code will give you the distance in meters.

上面的代码将给出以米为单位的距离。

Once you will get the distance between the two annotation through the coordinate then you can implement the same as per your requirement.

一旦您将通过坐标获得两个注释之间的距离,那么您就可以按照您的需求实现相同的实现。

#2


0  

It seems locations is an array of NSDictionary. In the for loop you cast the elements to MapPin* but that just makes the compiler happy. Accessing obj.coordinate.latitude will call selector coordinate on object obj. And the exception tells us its an NSDictionary object.

位置似乎是NSDictionary的数组。在for循环中,您将元素强制转换为MapPin*,但这只会让编译器感到高兴。访问obj.coordinate。纬度将调用对象obj上的选择器坐标。这个异常告诉我们它是一个NSDictionary对象。

Check what kind of object locations really is at runtime and change your code accordingly.

检查运行时的对象位置,并相应地修改代码。

#1


0  

Use this set of code to get the distance between two latitude and longitude.

使用这组代码来获取纬度和经度之间的距离。

In Objective C

在Objective - C

@class CLLocation

CLLocation *location1 =[[CLLocation alloc] initWithLatitude:[@"23.039698" doubleValue] longitude:[@"77.571663" doubleValue]];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:[@"23.0422" doubleValue] longitude:[@"72.5644" doubleValue]];
CLLocationDistance distanceMeter = [location distanceFromLocation:location2];

And in Swift

和迅速

import CoreLocation

let location1 = CLLocation(latitude: 5.0, longitude: 5.0)
let location2 = CLLocation(latitude: 5.0, longitude: 3.0)

let distanceInMeters = location1.distance(from: location2)

The above code will give you the distance in meters.

上面的代码将给出以米为单位的距离。

Once you will get the distance between the two annotation through the coordinate then you can implement the same as per your requirement.

一旦您将通过坐标获得两个注释之间的距离,那么您就可以按照您的需求实现相同的实现。

#2


0  

It seems locations is an array of NSDictionary. In the for loop you cast the elements to MapPin* but that just makes the compiler happy. Accessing obj.coordinate.latitude will call selector coordinate on object obj. And the exception tells us its an NSDictionary object.

位置似乎是NSDictionary的数组。在for循环中,您将元素强制转换为MapPin*,但这只会让编译器感到高兴。访问obj.coordinate。纬度将调用对象obj上的选择器坐标。这个异常告诉我们它是一个NSDictionary对象。

Check what kind of object locations really is at runtime and change your code accordingly.

检查运行时的对象位置,并相应地修改代码。