EDIT: I USE ARC IN PROJECT
I load annotations from plist like this:
[NSThread detachNewThreadSelector:#selector(loadPList) toTarget:self withObject:nil];
...
- (void) loadPList
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:#"test.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; // memory leak here
NSMutableArray *annotations = [[NSMutableArray alloc]init];
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"blackKey"])
{
NSArray *ann = [dict objectForKey:#"Black"];
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];
}
}
});
}
All loads fine, but Memory leak tool shows me an leak.
You need to put the #autoreleasepool at the start of your method - with that dictionaryWithContentsOfFile: call outside of it, you're creating an autoreleased object without a pool, so it'll leak. Per the threading programming guide:
If your application uses the managed memory model, creating an
autorelease pool should be the first thing you do in your thread entry
routine. Similarly, destroying this autorelease pool should be the
last thing you do in your thread.
Also, can I ask why you to use NSThread for loading the plist rather than a dispatch_async()using a global queue? I don't often see dispatch_async() nested inside a thread detachment, so just curious.
EDIT:
To fix your memory leak, without disturbing your thread / GCD hybrid, call your method like this:
[NSThread detachNewThreadSelector:#selector(loadPList) toTarget:self withObject:nil];
And implement it like this:
- (void) loadPList
{
#autoreleasepool {
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:#"test.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; // memory leak here
NSMutableArray *annotations = [[NSMutableArray alloc]init];
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"blackKey"])
{
NSArray *ann = [dict objectForKey:#"Black"];
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];
}
}
}
);
}
}
If nothing else, you need an autorelease pool. To quote from the documentation for detachNewThreadSelector, "the method aSelector is responsible for setting up an autorelease pool for the newly detached thread and freeing that pool before it exits."
Personally, I'd probably just invoke loadPlist via GCD rather than detachNewThreadSelector, and then you don't need to worry about an autorelease pool:
dispatch_async(get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self loadPlist];
});
NSMutableArray *annotations = [[NSMutableArray alloc]init]; // Never released
NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ; // Never released
MyAnnotation *myAnnotation = [[MyAnnotation alloc] init]; // Never released
Your method should look like this:
- (void) loadPList {
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:#"test.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; // memory leak here
NSMutableArray *annotations = [[[NSMutableArray alloc] init] autorelease];
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray * annotationsToRemove = [[mapView.annotations mutableCopy] autorelease];
[annotationsToRemove removeObject:mapView.userLocation] ;
[mapView removeAnnotations:annotationsToRemove] ;
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"blackKey"])
{
NSArray *ann = [dict objectForKey:#"Black"];
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] autorelease];
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];
}
}
});
}
Related
I have a function which prepares data for my UITableView:
- (void) SearchFromMyPosition {
TitleLabelSort = [[NSMutableArray alloc] init];
DistanceLabelSort = [[NSMutableArray alloc] init];
TagValueSort = [[NSMutableArray alloc] init];
DistanceUnitSort = [[NSMutableArray alloc] init];
LatArraySort = [[NSMutableArray alloc] init];
LngArraySort = [[NSMutableArray alloc] init];
// loading
[activityIndicatorObject startAnimating];
NSNumber *latNr1 = [[NSUserDefaults standardUserDefaults] valueForKey:#"api_lat"];
NSNumber *lngNr1 = [[NSUserDefaults standardUserDefaults] valueForKey:#"api_lng"];
double lat1 = [latNr1 doubleValue];
double lng1 = [lngNr1 doubleValue];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *array = [APIConnection GetDataFromUrlAuth];
dispatch_async(dispatch_get_main_queue(), ^{
for (NSString *item in array) {
NSArray *coords = [[[array valueForKey:item] valueForKey:#"location"] componentsSeparatedByString:#"|"];
double lat2 = [[coords objectAtIndex:0] doubleValue];
double lng2 = [[coords objectAtIndex:1] doubleValue];
CLLocation *oldLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
[LatArraySort addObject:[NSString stringWithFormat:#"%f",lat2]];
[LngArraySort addObject:[NSString stringWithFormat:#"%f",lng2]];
[TitleLabelSort addObject:[[array valueForKey:item] valueForKey:#"name"]];
[DistanceLabelSort addObject:[NSString stringWithFormat:#"%f",meters]];
[TagValueSort addObject:item];
[self.myTableView reloadData];
[activityIndicatorObject stopAnimating];
}
});
});
}
This works but I need to sort results by DistanceLabelSort
For sorting CoreData results I use below function and works fine:
- (void) SetTableData {
TitleLabel = [[NSMutableArray alloc] init];
DistanceLabel = [[NSMutableArray alloc] init];
TagValue = [[NSMutableArray alloc] init];
DistanceUnit = [[NSMutableArray alloc] init];
LatArray = [[NSMutableArray alloc] init];
LngArray = [[NSMutableArray alloc] init];
// sorting
NSArray *sortedArray = [DistanceLabelSort sortedArrayUsingComparator:^(NSString *str1, NSString *str2) {
return [str1 compare:str2 options:NSNumericSearch];
}];
NSString *sortIdentStr = [[NSString alloc] init];
unsigned long sortIdent;
NSMutableArray *arrayUnit = [[NSMutableArray alloc] init];
for (int i = 0; i < sortedArray.count; i++) {
sortIdentStr = [sortedArray objectAtIndex:i];
sortIdent = [DistanceLabel indexOfObject:sortIdentStr];
arrayUnit = [self unitStr:sortIdentStr];
[TitleLabel addObject:[TitleLabelSort objectAtIndex:sortIdent]];
[TagValue addObject:[TagValueSort objectAtIndex:sortIdent]];
[LatArray addObject:[LatArraySort objectAtIndex:sortIdent]];
[LngArray addObject:[LngArraySort objectAtIndex:sortIdent]];
[DistanceLabel addObject:[arrayUnit objectAtIndex:0]];
[DistanceUnit addObject:[arrayUnit objectAtIndex:1]];
[self.myTableView reloadData];
[activityIndicatorObject stopAnimating];
}
}
But if I try use this for sorting data from external api function SetTableData is done before I get results from external api.
Finally I found solution and maybe this will be helpful for others:
I added function which checks if the task is done:
- (void)SearchFromMyPositionWithSuccess:(void (^)(void))successHandler failure:(void (^)(void))failureHandler {
TitleLabelSort = [[NSMutableArray alloc] init];
DistanceLabelSort = [[NSMutableArray alloc] init];
TagValueSort = [[NSMutableArray alloc] init];
DistanceUnitSort = [[NSMutableArray alloc] init];
LatArraySort = [[NSMutableArray alloc] init];
LngArraySort = [[NSMutableArray alloc] init];
// loading
[activityIndicatorObject startAnimating];
NSNumber *latNr1 = [[NSUserDefaults standardUserDefaults] valueForKey:#"api_lat"];
NSNumber *lngNr1 = [[NSUserDefaults standardUserDefaults] valueForKey:#"api_lng"];
double lat1 = [latNr1 doubleValue];
double lng1 = [lngNr1 doubleValue];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *array = [APIConnection GetDataFromUrlAuth];
dispatch_async(dispatch_get_main_queue(), ^{
int i = 0;
for (NSString *item in array) {
i++;
NSArray *coords = [[[array valueForKey:item] valueForKey:#"location"] componentsSeparatedByString:#"|"];
double lat2 = [[coords objectAtIndex:0] doubleValue];
double lng2 = [[coords objectAtIndex:1] doubleValue];
CLLocation *oldLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
[LatArraySort addObject:[NSString stringWithFormat:#"%f",lat2]];
[LngArraySort addObject:[NSString stringWithFormat:#"%f",lng2]];
[TitleLabelSort addObject:[[array valueForKey:item] valueForKey:#"name"]];
[DistanceLabelSort addObject:[NSString stringWithFormat:#"%f",meters]];
[TagValueSort addObject:item];
if (i == array.count) {
successHandler();
}
[activityIndicatorObject stopAnimating];
}
});
});
}
And wait for success in this function:
-(void) SearchFromMyPosition {
[self SearchFromMyPositionWithSuccess: ^{
[self SetTableData];
NSLog(#"success");
} failure:^{
NSLog(#"failure");
}];
}
This solves my case :)
Now I loop through one Array at once and calculate distance like this:
- (void)calculateDistance
{
ann = [dict objectForKey:#"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];
// 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];
// Adding distance to dictionaries
if (distance > 1000) {
NSString *dist = [NSString stringWithFormat:#"%.2f km.", distance/1000];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:#"Distance"];
}
else{
NSString *dist = [NSString stringWithFormat:#"%4.0f m.", distance];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:#"Distance"];
}
}
}
My data structure is:
How to loop through all Array's at once? I have Array which contains all my Array's named "resultArray", but this code doesn't work:
ann = [dict objectForKey:resultArray];
NSLog(#"%#", resultArray);
2013-05-05 10:57:03.643 testApp[5708:907] (
Black,
Green,
Orange,
Blue,
Darkblue
)
I guess you want to enumerate through the keys stored in resultArray and calculate the distance and add that calculated values to it.
- (void)calculateDistance
{
//Enumerates through resultArray
for (NSString *key in resultArray) {
//ann array is considered as an instance of NSMutableArray
ann = dict[key];
for(int i = 0; i < [ann count]; i++) {
NSMutableDictionary *inDict = [ann[i] mutableCopy];
NSString *coordinates = inDict[#"Coordinates"];
NSArray *coordinateComponents = [coordinates componentsSeparatedByString:#","];
double realLatitude = [coordinateComponents[1] doubleValue];
double realLongitude = [coordinateComponents[0] doubleValue];
// 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];
// Adding distance to dictionaries
if (distance > 1000) {
NSString *dist = [NSString stringWithFormat:#"%.2f km.", distance/1000];
[inDict setValue:dist forKey:#"Distance"];
}
else{
NSString *dist = [NSString stringWithFormat:#"%4.0f m.", distance];
[inDict setValue:dist forKey:#"Distance"];
}
//Inserting the modified values to the main array
[ann replaceObjectAtIndex:i withObject:inDict];
}
}
}
ann = [dict objectForKey:resultArray];
for(NSArray *colourArray in ann)
{
for(NSDictionary *itemDictionary in colourArray)
{
NSLog(#"Coordinates = %#",[itemDictionary objectForKey:#"Coordinates"]);
NSLog(#"Name = %#",[itemDictionary objectForKey:#"Name"]);
NSLog(#"Address = %#",[itemDictionary objectForKey:#"Address"]);
}
}
Hope it will help you .
I have an array of strings that I want to use as the filter for another array of dictionaries that is created from a plist. For example, if I had a plist of dictionaries that looked like so:
Key: Value:
car1 audi
car2 bmw
car3 bmw
car4 audi
car5 jaguar
and my array of strings was "audi, jaguar". How would I code it so that I can create a new array that would return "car1, car4, car5"? Hope this makes sense. Or better yet, how can I walk down this dictionary and filter it based on a value and then create a new array of dictionaries to use.
Code:
-(void)plotStationAnnotations {
desiredDepartments = [[NSMutableArray alloc] init];
BOOL tvfrSwitchStatus = [[NSUserDefaults standardUserDefaults] boolForKey:#"tvfrSwitchStatus"];
BOOL hfdSwitchStatus = [[NSUserDefaults standardUserDefaults] boolForKey:#"hfdSwitchStatus"];
if (tvfrSwitchStatus) {
NSString *tvfr = #"TVF&R";
[desiredDepartments addObject:tvfr];
}
if (hfdSwitchStatus) {
NSString *hfd = #"HFD";
[desiredDepartments addObject:hfd];
}
NSLog(#"Array 1 = %#", desiredDepartments);
NSString *path = [[NSBundle mainBundle] pathForResource:#"stationAnnotations" ofType:#"plist"];
NSMutableArray *anns = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSMutableArray *newDictionaryArray = [NSMutableArray array];
for (NSDictionary *dictionary in anns) {
for (NSString *string in desiredDepartments) {
if ([dictionary allKeysForObject:string]) {
[newDictionaryArray addObject:dictionary];
break;
}
}
}
NSLog(#"Array = %#", keyMutableArray);
for (int i = 0; i < [keyMutableArray count]; i++) {
float realLatitude = [[[keyMutableArray objectAtIndex:i] objectForKey:#"latitude"] floatValue];
float realLongitude = [[[keyMutableArray objectAtIndex:i] objectForKey:#"longitude"] floatValue];
StationAnnotations *myAnnotation = [[StationAnnotations alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [[keyMutableArray objectAtIndex:i] objectForKey:#"station"];
myAnnotation.subtitle = [[keyMutableArray objectAtIndex:i] objectForKey:#"department"];
[mapView addAnnotation:myAnnotation];
}
}
May be something like
NSArray *array1;
if([array1 containsObject : someValue])
can help. someValue can be your values you want to check if they exist in array1.
You can do something like this to filter by keys:
NSArray *keysToLookFor = [NSArray arrayWithObjects:#"car1", #"car4", #"car5", nil];
NSArray *foundObjects = [dictionary objectsForKeys:keysToLookFor notFoundMarker:nil];
Or something like this to filter by values:
NSString *valueToLookFor = #"Audi";
NSArray *keyArray = [dictionary allKeysForObject:valueToLookFor];
// To filter by multiple values
NSArray *valuesToFilterBy = [NSArray arrayWithObjects:#"Bmw", #"Audi", nil];
NSMutableArray *keyMutableArray = [NSMutableArray array];
for (NSString *string in valuesToFilterBy) {
[keyMutableArray addObjectsFromArray:[dictionary allKeysForObject:string]];
}
Updated answer for dictionaries in arrays:
NSArray *dictionaryArray; // The array of dictionaries that you have
NSMutableArray *newDictionaryArray = [NSMutableArray array];
NSArray *valuesToFilterBy = [NSArray arrayWithObjects:#"Bmw", #"Audi", nil];
for (NSDictionary *dictionary in dictionaryArray) {
for (NSString *string in valuesToFilterBy) {
if ([dictionary allKeysForObject:string]) {
[newDictionaryArray addObject:dictionary];
break;
}
}
}
I load my annotations from plist and loop categories
NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
for(path in dict){
NSString *theCategory;
theCategory = [NSString stringWithFormat:#"%#", path];
NSLog(#"%#", path);
NSArray *ann = [dict objectForKey:theCategory];
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];
}
}
Then I have an switch in my settings
- (IBAction)saveSwitch:(id)sender
{
NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
[defs1 setBool: blackSwitch.on forKey: #"blackKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I need to limit display of annotations from specific category by this switch. How it can be done, if I loop categories?
Inside your first loop, do this:
for(NSString *category in dict){
if ([category isEqualToString:#"Whatever"]) {
//continue?
NSArray *ann = [dict objectForKey:category];
BOOL blackKeyStatus = [[NSUserDefaults standardUserDefaults]boolForKey:#"blackKey"];
if (blackKeyStatus) {
//switch ON
//act appropriatelyForOnValue
//begin Loop 2 here?
} else {
//switch OFF
//act appropriately
}
} else {
//do something else?
}
If you're needing to test for a specific condition, do whatever is necessary to identify if that condition is met.
Why am I unable to remove my annotations from mapview?
My code:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"blackKey"])
{
NSLog(#"Black is on");
NSArray *ann = [dict objectForKey:#"Category1"];
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"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"blackKey"])
{
NSLog(#"Black is on");
[mapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
} else
{
NSLog(#"Black is off");
[self.mapView removeAnnotation:myAnnotation];
}
}
}
else
{
//Do nothing
}
}
[self.mapView removeAnnotation:myAnnotation]; does not work for me
There is nothing to remove. You create the annotation, and then based off the check for blackKey, you EITHER add or remove it. But when you remove it, you never added it prior to that.