【问题标题】:Mapview Showing Closest location地图视图显示最近的位置
【发布时间】:2013-08-06 22:00:14
【问题描述】:
为具有多个地点的企业构建 iPhone 应用程序。我有一个显示所有位置的地图视图,我让它在地图上显示位置。缩小你会看到我指定的所有位置。问题是我需要它首先显示离客户当前位置最近的位置。
关键点:
1. 使用属性列表(config.plist)来指定位置。
2. 它确实显示了当前位置的蓝点,这是准确的。当缩小并选择我设置的任何位置时,它会带您到谷歌地图,从当前位置到特定的营业地点都是准确的。
3. 这是为 iPhone 使用的当然是 xcode。
谢谢。
【问题讨论】:
标签:
iphone
ios
mkmapview
currentlocation
【解决方案1】:
只是让您从一些代码开始,下面是一个迭代所有位置并返回最近的位置的方法示例。
-(YourModel*)getClosestLocation
{
CLLocation *myLocation = currentLocation //This is the current location you should get from the GPS
NSArray *allLocations = myModelArray; //array with all locations you want to compare
YourModel *closestLocation = nil;
CLLocationDistance closestDistance = CGFLOAT_MAX;
for (YourModel* model in allLocations)
{
CLLocation *modelLocation = [[CLLocation alloc] initWithLatitude:model.latitude longitude:model.longitude];
CLLocationDistance distance = [myLocation distanceFromLocation:modelLocation];
if (distance < closestDistance) {
closestLocation = modelLocation;
closestDistance = distance;
}
}
return closestLocation;
}
如果有帮助,请提供一些反馈。
【解决方案2】:
这看起来很简单,遍历位置,计算到当前位置的距离,找到最短的位置(在迭代过程中跟踪它),将地图设置为缩放以显示用户位置和最近位置的边界框。