简体   繁体   English

IOS中地图中当前位置最近的位置

[英]Nearest location from current location in map in IOS

Have a button on a mapview which is used to calculate the nearest location from the locations i have plotted on map. 在mapview上有一个按钮,用于从我在地图上绘制的位置计算最近的位置。 I have applied some code but when i click the button the app crashes by giving this error, 我已经应用了一些代码,但是当我单击按钮时,由于出现此错误,应用程序崩溃了,

[__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] *由于未捕获的异常'NSInvalidArgumentException'而终止了应用程序,原因:'-[__ NSDictionaryI坐标]:无法识别的选择器已发送到实例0x174e61b40' *第一掷调用堆栈:(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:与类型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 错误屏幕截图为, 在此处输入图像描述

Use this set of code to get the distance between two latitude and longitude. 使用此代码集获取两个纬度和经度之间的距离。

In Objective C 目标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 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. 通过坐标获得两个注释之间的距离后,即可根据需要实施相同的操作。

It seems locations is an array of NSDictionary . 看来locationsNSDictionary的数组。 In the for loop you cast the elements to MapPin* but that just makes the compiler happy. for循环中,将元素MapPin*MapPin*但这只会使编译器满意。 Accessing obj.coordinate.latitude will call selector coordinate on object obj . 访问obj.coordinate.latitude将在对象obj上调用选择器coordinate And the exception tells us its an NSDictionary object. 异常告诉我们它是一个NSDictionary对象。

Check what kind of object locations really is at runtime and change your code accordingly. 检查运行时真正位于哪种对象locations ,并相应地更改代码。

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

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