0

trying to figure out if it is possible when a button is pressed to go to a certain longitude and latitude in mapView and drop a button. I copied my code for a "newAnnotation" pin which previously worked, but realize now it might not be working inside of the code of my button. Here is my button code:

-(IBAction)buttonPressed 
{
CLLocationCoordinate2D location;
MKCoordinateSpan span;
location.latitude = (double) 44.4758;
    location.longitude = (double) -73.2125;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;


    MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"BCA" 
    andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
[newAnnotation release];


themapView = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];
[self.view addSubview:themapView.view]; 
}

I know that the button WORKS and that it actually goes to the mapView and is processing the coordinates, but that it just isn't dropping the pin with the title. Currently there are no errors as my code stands. If you need to see other code, let me know. Thanks a million.

Craig
  • 8,093
  • 8
  • 42
  • 74
Greg
  • 276
  • 1
  • 3
  • 25
  • I know that the button WORKS and that it actually goes to the mapView and is processing the coordinates, but that it just isn't dropping the pin with the title. Currently there are no errors as my code stands. If you need to see other code, let me know. Thanks a million. – Greg Sep 16 '12 at 15:00

2 Answers2

1

I think your problem is that you are adding an annotation to a map view that is not visible yet. You have to first add the map view view as a subview then add the annotation. Here is what the code would look like:

-(IBAction)buttonPressed 
{
CLLocationCoordinate2D location;
MKCoordinateSpan span;
location.latitude = (double) 44.4758;
    location.longitude = (double) -73.2125;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;

MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"BCA" 
andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
[newAnnotation release];


themapView = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];
[self.view addSubview:themapView.view]; 

}

UPDATE:

Can we see the code for MapViewAnnotation. It should be an NSObject class which adopts MKAnnotation like this <MKAnnotation> and you should declare and synthesize three properties: title, subtitle if you like, and of course coordinate

Another problem may be that you are adding MapView's view as a subview. It might be better to put this code in MapView and present MapView as a view controller

I also do not understand why you are adding an annotation to self.mapView and then adding a subview over it.....

MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • Thanks! Just tried it but no cigar though. It is the weirdest thing! – Greg Sep 16 '12 at 15:28
  • You got it in your last part. I was accidentally adding MapView's view as a subview and it was conflicting. That is the number one problem here. Now that I know that I can try to work around it. My question is: is it even possible to press a button and then go to a mapview and have a pin drop on the coordinates that passed through that button? Sure, anyone can plug coordinates into a viewDidLoad for the mapView itself and have it happen, but I'm talking about each button having its own coordinates and passing it to the mapView. I have seen no examples of this whatsoever. – Greg Sep 17 '12 at 15:47
  • If the answer helped, you can upvote and tick. You would just need to create a few buttons connected to different actions and in these different IBAction methods is where you put code to drop a pin with specific coordinates.... – MCKapur Sep 17 '12 at 22:46
  • 1
    UPDATE: the short of it here as I have learned is that the same reference to the mapView class cannot be the same one to process the drop pin function, and so you would be correct that probably another button would need to be added somewhere, but that would unfortunately defeat the purpose of my app, which is to send coordinates to the mapview from the previous view :( It sounds like the only way this app is going to work is to have a button for individual coordinates in that very same mapView that the button is to be dropped in. – Greg Sep 17 '12 at 23:42
  • 1
    also: i read somewhere that parameters cannot be passed thru buttons and so it sounds like my problems come from that, because the button DOES access the mapview, it just doesnt drop the pin. mystery solved i believe – Greg Sep 18 '12 at 01:18
0

You're adding your annotation to self.mapview, then making a separate map and adding its view as a subview, which is weird.

If your self.mapview is already on screen then you can remove the last two lines of your function. If not then your function might need to be changed to be more like this

-(IBAction)buttonPressed 
{
    CLLocationCoordinate2D location;
    MKCoordinateSpan span;
    location.latitude = (double) 44.4758;
    location.longitude = (double) -73.2125;
    span.latitudeDelta=0.2;
    span.longitudeDelta=0.2;


    themapView = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];
    [self.view addSubview:themapView]; 

    MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"BCA" 
andCoordinate:location];
    [themapView addAnnotation:newAnnotation];
    [newAnnotation release];

}

Important changes are that you're adding the annotation to themapView that you've just initialised, and that you're adding themapView (not its .view) to the current .view. You may also need to recenter your map.

Craig
  • 8,093
  • 8
  • 42
  • 74