0
我環路不同的值來計算的註解從用戶位置的距離是這樣的:無法通過<code>NSDictionaries</code>設置不同的字典
CLLocation *pinLocation = [[CLLocation alloc]
initWithLatitude:realLatitude
longitude:realLongitude];
CLLocation *userLocation = [[CLLocation alloc]
initWithLatitude:mapView.userLocation.coordinate.latitude
longitude:mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
結果:
NSLog(@"Distance: %4.0f m.", distance);
2013-05-04 15:58:53.301 testApp[3194:907] Distance: 65758 m.
2013-05-04 15:58:53.304 testApp[3194:907] Distance: 91454 m.
2013-05-04 15:58:53.308 testApp[3194:907] Distance: 248726 m.
2013-05-04 15:58:53.310 testApp[3194:907] Distance: 297228 m.
2013-05-04 15:58:53.313 testApp[3194:907] Distance: 163058 m.
然後我嘗試添加距離用於字典
NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
[ann setValue:dist forKey:@"Distance"];
但它只添加最後一個距離(距離:163058米)爲所有字典。
如何爲不同的字典設置不同的值?
編輯:我的環
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blueKey"])
{
ann = [dict objectForKey:@"Blue"];
[resultArray addObject:@"Blue"];
for(int i = 0; i < [ann count]; i++) {
NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];
double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];
MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);
myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];
[mapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
// Calculating distance
CLLocation *pinLocation = [[CLLocation alloc]
initWithLatitude:realLatitude
longitude:realLongitude];
CLLocation *userLocation = [[CLLocation alloc]
initWithLatitude:mapView.userLocation.coordinate.latitude
longitude:mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
// NSLog(@"Distance: %4.0f m.", distance);
NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
[ann setValue:dist forKey:@"Distance"];
}
}
它不斷地循環在我的其他循環的for(int i = 0; I <[安計數];我++){ – 2013-05-04 13:43:58
@PavelKaljunen : 我不懂誒。你能詳細說明嗎? – Bhavin 2013-05-04 13:48:05
請看我編輯的問題 – 2013-05-04 13:51:59