2

Struggling bad here on something I am now exhausted with. There are two parts here:

1) I am trying to get a pin removed if the user searches the same coordinate that is already identified and pinned on ViewDidLoad. This part WORKS. The non operable part is #2.

2) When the user searches ANY OTHER location, I want a pin added, i.e. [self.mapView addAnnotation:addAnnotation]; My code is below.

- (IBAction) showAddress // THIS IS A BUTTON WHICH LOCATES COORDINATES
{

[addressField resignFirstResponder];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;

CLLocationCoordinate2D location = [self addressLocation];
region.span=span;
region.center=location;

[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];

if (location.longitude = (double) -73.2125) 
{   
    addAnnotation = [[AddressAnnotation alloc]initWithCoordinate:location]; 
    [self.mapView removeAnnotation:addAnnotation];
}

else  
{
    addAnnotation = [[AddressAnnotation alloc]initWithCoordinate:location]; 

    [self.mapView addAnnotation:addAnnotation];
    [addAnnotation release];
}
}

Any thoughts? Thanks guys!

Greg
  • 276
  • 1
  • 3
  • 25
  • Are you saying that you call to removeAnnotation actually works? I can't see how it would since you're removing an annotation that you have only created in the line above and not even added to the map yet. – Craig Sep 13 '12 at 21:20
  • (I'm not harassing you, I swear - just looking over your other questions per our housekeeping discussion elsewhere. :-)) – Joshua Nozzi Oct 26 '12 at 13:52

1 Answers1

1

This line:

if (location.longitude = (double) -73.2125)

does an assignment (=), not a comparison (==).

Since the assignment is always successful, it always goes to the removeAnnotation part.


However, I don't recommend comparing doubles (or any floating-point numbers) using ==.
I suggest checking instead if the two numbers are within some small distance of each other.
See this answer for an example.

Community
  • 1
  • 1