从现有坐标地图包中查找最近的位置 [英] Find nearest location from existing coordinates mapkit

查看:91
本文介绍了从现有坐标地图包中查找最近的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为拥有多家商店的客户开发iphone应用程序(目标C).我有一个数组中所有商店(20)的坐标(纬度,经度).

I am in the process of developing an iphone app (objective C) for a client who has multiple stores. I have the coordinates (latitude,long) of all the stores (20) in an array.

此刻,我正在考虑遍历商店坐标数组,并获取从用户当前位置到商店位置的距离,然后将它们添加到数组中并以最小距离进行排序.

At the moment I am thinking of looping through the array of stores' coordinates and get the distance from the user's current location to the store's location and then add them into array and does sorting on the lowest distance.

这是正确的方法还是非常耗资源?

Is this a correct approach or very resource hungry?

最接近SOF的问题是这个,但它在python中: 提取最近的商店

The nearest question of SOF was this one but it is in python: extracting nearest stores

预先感谢您的建议.

推荐答案

要在获取的数据中查找最近的位置,您需要计算从当前位置到拥有的每个位置的距离.然后,只需对这些数据进行排序,您就会从现有的坐标地图包中获得最近的位置.

To find nearest location among fetched data, you need to calculate distance from current location to each location you are having. And then, just sort those data and you will get the nearest location from existing coordinate mapkit.

以下是找出距离的方法...

Following is a method to find out the distance...

-(float)kilometresBetweenPlace1:(CLLocationCoordinate2D) currentLocation andPlace2:(CLLocationCoordinate2D) place2 
{
    CLLocation *userLoc = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude];
    CLLocation *poiLoc = [[CLLocation alloc] initWithLatitude:place2.latitude longitude:place2.longitude];  
    CLLocationDistance dist = [userLoc getDistanceFrom:poiLoc]/(1000*distance);
    NSString *strDistance = [NSString stringWithFormat:@"%.2f", dist];
    NSLog(@"%@",strDistance);
    return [strDistance floatValue];
}

这篇关于从现有坐标地图包中查找最近的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆