Greg
Greg

Reputation: 276

mapView "addAnnotation:addAnnotation" NOT dropping pin as desired with IF/ELSE statements

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!

Upvotes: 2

Views: 165

Answers (1)

user467105
user467105

Reputation:

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.

Upvotes: 1

Related Questions