मैं पाश NSDictionaries
उपयोगकर्ता स्थान से इस तरह एनोटेशन की दूरी की गणना करने के लिए:
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"];
}
}
उत्तर:
उत्तर № 1 के लिए 1आप कुछ ऐरे में सभी वैल्यूज को जोड़ सकते हैं और फिर आप इसका इस्तेमाल अलग-अलग डॉक्स में सेव करने के लिए कर सकते हैं।
इस लाइन के बाद:
// NSLog(@"Distance: %4.0f m.", distance);
जोड़ें:
NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:@"Distance"];