2013-08-06 55 views
0

建立一个iPhone应用程序的业务与多个位置。我有一个显示所有位置的地图视图,我用它来显示地图上的位置。缩小显示您看到我指定的所有位置。问题是我需要它首先显示客户当前位置最近的位置。Mapview显示最近的地段

要点: 1.使用property-list(config.plist)指定位置。 2.它确实显示了当前位置的蓝点,这是准确的。当缩小和选择任何位置时,我已将它设置为将您带到Google地图,并且从当前位置到该特定商户位置都是准确的。 3.这是iPhone的使用当然xcode。

谢谢。

回答

0

这似乎很简单,遍历位置,计算到当前位置的距离,找到最短的一个(在迭代期间跟踪它),将地图设置为缩放以显示用户位置和最近位置的边界框。

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

请给予一些反馈,如果它有帮助。