在ios中按升序排序某些金额
我尝试按升序对一些值(距离)进行排序,但没有成功,我尝试了以下方法:在ios中按升序排序某些金额,ios,objective-c,Ios,Objective C,我尝试按升序对一些值(距离)进行排序,但没有成功,我尝试了以下方法: for (ChargingSpots *spot in chSpot) { CLLocation *thePoint = ([[CLLocation alloc]initWithLatitude:[spot.LocationLat doubleValue] longitude:[spot.LocationLong doubleValue]]); CLLocationDistanc
for (ChargingSpots *spot in chSpot)
{
CLLocation *thePoint = ([[CLLocation alloc]initWithLatitude:[spot.LocationLat doubleValue] longitude:[spot.LocationLong doubleValue]]);
CLLocationDistance distance = [thePoint distanceFromLocation:currentLocation];
// spot.currentDistance = [NSNumber numberWithDouble:distance];
spot.currentDistance = @(distance);
// NSLog(@"%@",dist);
}
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"currentDistance" ascending:YES];
[chSpot sortedArrayUsingDescriptors:@[sort]];
我想做的是把距离放在一个数组中,然后按升序排序。
然后我想在最近的3点上放大
for (int i=0; i<chSpot.count; i++)
{
ChargingSpots *spot = [chSpot objectAtIndex:i];
for (int i=0; i<3; i++)
{
bounds = [bounds includingCoordinate:CLLocationCoordinate2DMake([spot.LocationLat doubleValue],[spot.LocationLong doubleValue])];
}
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:currentLocation.coordinate.latitude
longitude:currentLocation.coordinate.longitude
zoom:4];
mapView_.camera = camera;
}
for(inti=0;iTry
更新了缩放的答案
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"currentDistance"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [chSpot sortedArrayUsingDescriptors:sortDescriptors];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[[sortedArray firstObject] valueForKey:@"latitude"]
longitude:[[sortedArray firstObject] valueForKey:@"longitude"]
zoom:4];
mapView_.camera = camera;
嘿,我认为你没有将距离放入数组中。你修改了当前实例,但没有更新cgSpot数组。sortedarray使用描述符:
返回一个NSArray
排序。返回你不使用的值。如果你想保留chSpot
,如果它是NSMutableArray
,请使用sortUsingDescriptors:
太好了,非常感谢。现在我还有一个问题,我需要在至少3个点(最近的点)上进行缩放将地图位置更改为该点,然后更改mapI上的缩放级别修改了我的代码,以便您可以检查我尝试的方式,但由于我无法结束循环而无法工作…看一看,您不应该在for循环中初始化相机,检查更新的答案这是循环没有以3结束,它给出了这是21的所有元素
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"currentDistance"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [chSpot sortedArrayUsingDescriptors:sortDescriptors];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[[sortedArray firstObject] valueForKey:@"latitude"]
longitude:[[sortedArray firstObject] valueForKey:@"longitude"]
zoom:4];
mapView_.camera = camera;