0

为具有多个位置的企业构建 iPhone 应用程序。我有一个显示所有位置的地图视图,我让它在地图上显示位置。缩小你会看到我指定的所有位置。问题是我需要它首先显示离客户当前位置最近的位置。

要点: 1. 使用property-list (config.plist) 指定位置。2. 它确实显示了当前位置的蓝点,这是准确的。当缩小并选择我设置的任何位置时,它会带你到谷歌地图,从当前位置到特定的营业地点都是准确的。3. 这当然适用于使用 xcode 的 iPhone。

谢谢。

4

2 回答 2

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;
}

请提供一些反馈,如果它有帮助。

于 2013-08-07T01:27:25.520 回答
0

这看起来很简单,遍历位置,计算到当前位置的距离,找到最短的位置(在迭代过程中跟踪它),将地图设置为缩放以显示用户位置和最近位置的边界框。

于 2013-08-07T00:35:23.843 回答