How to reload MKAnnotations without reloading MapView? - iphone

I have an app which searches using a Web service for informations about stations, then I get the response JSON. But each time I change the position of the center I have to reload the pins and add new ones, even if they are already on the map.
I can get the response JSON, get the information to display and create the annotations. The problem is when I move the center, it reloads the annotations over the others.

The problem is I was inserting that method in the method that makes the connection to the web service or when i receive the response...
But I inserted the method in this one in the first line:
- (MKAnnotationView *)mapView:(MKMapView *)mapViewStation viewForAnnotation:(id <MKAnnotation>)annotation
[map removeAnnotations:map.annotations];
//Code
It rocks. Thanks

Related

Is it possible to add custom annotation without pin in xcode?

I need the annotation in the center of map just show me the title and no pin.
Is it possible to do so in Xcode? if yes how can I do this?
Yes, it's possible to create custom views instead of pins - see a great example MapCallouts from Apple, showing how to achieve this http://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009746
in general, you need to implement a - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation method and return a custom view for you annotation (i believe the one with title and so on);

How do I show multiple MKAnnotations with the callout?

I want to add multiple MKAnnotations with the callout showing. I searched a few different threads on S.O and could not find a way to do it. Has anyone figured out how to add multiple pins with the callout visible?
Ok!
You should use [mapView selectAnnotation:currentAnnotation animated:FALSE]
But be sure to place this in the - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
delegate method
Trigger MKAnnotationView callout

callout bubble MKMapView move problem

Im currently developping an App which uses a MKMapView. Wenn the map is moved I perform server request to get new Annotations based on the coordinates.
Wenn an annotation is clicked it sometines moves the map. In this case i don't want to perform a Request.
Has somebody a solution how to know if it was an callout buble which moved the map ?
I tried with - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ without success !
Look at this answer where I explain several methods to track that the MKMapView has moved.
That answers a different problem, but I think that might help you.

MapKit custom annotations being added to map, but are not visible on the map

I have an interface with a mapView and UITableView. Data is loaded from a server, and the annotations are created and added to the map with
[mapView addAnnotation:truck]
the tableview is then populated using the array thats retured from
[mapView annotations]
once this process is completed, i check the number of annotations on the map with [[mapView annotations] count] called whenever i click on a cell in the table and its equal to the number it ought to be, so all the annotations are getting added onto the mapView, but for some reason I cant see any annotations in the simulator.
The images are named just as they are assigned in the custom AnnotationView, the loadAnnotation function is done properly, etc... i dont know what it could be but ive looked at the associate between the image file and wheres its loaded a hundred times to find a discrepancy, but it all looks fine.
One interesting point is that when i print and coordinate value after clicking on the cell (remember this data comes straight from [mapView annotations], it looks good... but for whatever reason the annotation view isnt being displayed.
so i suppose if i could have the answer to one question it would be, what are possible causes for a mapView to contain several annotations, but to not show any on the map?
Thanks
EDITED WITH IMPORTANT ADDITIONAL INFO
There appears to be a disconnect between what is being displayed on my map in the simulator and what im seeing in the mapView object. For example, when i select a row in the tableView, I am calling:
[mapView setRegion:MKCoordinateRegionMake([annotation coordinate], MKCoordinateSpanMake(.01, .01)) animated:YES];
and this has no effect on the map in the simulator. I have checked that the coordinate being passed is actually a valid coordinate, but it doesnt seem to matter because the map is being unresponsive. I thought it could be something wrong with my .xib, but its all connected properly. delegate is the outlet and mapView as a referencing outlet.
Does anyone now understand what may be happening?
This really looks like missing images.
Try replacing your custom annotations with MKPinAnnotations to test, and see if your annotations are visible then.
Have you implemented the following delegate function ?
(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation;
This line:
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
was creating messing everything up. once i commented it out, the app began working fine.

multiple regionDidChangeAnimated calls - what gives?

I have a MKMapView inside a UITableView as a custom cell (don't ask ;) - don't know if it matters really), for which I register a regionDidChangeAnimated delegate method. This method gets called three times when the UITableView is loaded - once with the actual region and then two more times with a region that is way off. In the simulator, I consistently get a region with center (+37.43997405, -97.03125000). On the device, it seems to depend on the location reported by the location manager, which initializes the map view.
Why am I getting three regionDidChangeAnimated calls? And why are the center coordinates for the last two of them off?
This is the code I use to get the center coordinates:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
CLLocation *l = [[CLLocation alloc] initWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude];
(....)
I have set up a map view inside a custom table view cell and added that cell to a table view (although it definitely should not matter where/how the map view is displayed).
I do not see any unexpected calls to the regionDidChangeAnimated: delegate method.
I see calls to this method only when:
The user changes the position/zoom of the map, OR
Some code changes the center/span of the map
Are you sure that you are seeing unexpected calls? You are not using code to setup the region (center/span) of the map?