How to remove annotations added as MKAnnotationView objects - iphone-sdk-3.0

I am new to using MapKit framework.
I am adding an image to MKAnnotationView object at specific lat-long pair on map, say at (latA, longA).
For the next iteration, i need to remove the annotation added and add new annotation at different lat-long pair, say at (latB, longB).
Let me know of any help.
Thanks
#dity#

Step1: Remove the annotation
[self.mapView removeAnnotation:annotation];
Step2: Change/Recreate the annotation, and add it back
[self.mapView addAnnotation:annotation]

Related

The removeAnnotations for MKMapView

Why is it so useful to delete annotations?
[self.mapView removeAnnotations:[self.mapView annotations]];
It doesn't work.do this:
[self.mapView removeAnnotations:self.mapView.annotations];
The system definition:
- (void)removeAnnotations:(NSArray<id<MKAnnotation>> *)annotations;
If the question is really WHY, this is what you can find reading the official documentation :
If the annotation is currently associated with an annotation view, and
that view has a reuse identifier, this method removes the annotation
view and queues it internally for later reuse. You can retrieve queued
annotation views (and associate them with new annotations) using the
dequeueReusableAnnotationViewWithIdentifier: method.
Removing an
annotation object disassociates it from the map view entirely,
preventing it from being displayed on the map. Thus, you would
typically call this method only when you want to hide or delete a
given annotation.

MKAnnotation or MKMapItem

What's the difference between MKAnnotation and MKMapItem?
Which one to use when I want to display some interesting places on my map?
I have a list of objects that have latitude, longitude, title, description and photo so far and I'd like to have them on my map.
MKAnnotation is a MARKER meant for displaying stuff using a MKAnnotationView
so the procedure is:
add a MKAnnotation to let the map know that there is an entry
wait for the map's delegate call viewForAnnotation
set up a annotationView there that is to be shown
a good tutorial (IMO) that shows this with detailed code is at: http://www.codigator.com/tutorials/mapkit-tutorial-for-ios-beginners/
MKMapItem is only for the openMapWithItem api that opens the external app!
EDIT: MKMapItem has a placemark property which is a MKAnnotation - so if that's non-nil you can add it. (note that it might be nil)
MKAnnotation is a protocol, used to provide annotation-related information to a map view.
I think what you really meant to refer to was a MKAnnotationView, which is used to "present annotations visually in a map view".
A MKMapItem "encapsulates information about a specific point on a map. This information includes the map location and any other data that might be relevant, such as the name of a business at that location. Apps use this class to share map-related data with the Maps app."
If you want to show annotations for a specific point on your map, I suspect what you want to do is use a MKAnnotationView.

How to style Overlay on MapView

Image: ( http://photos.appleinsider.com/iOS5maps002.091111.jpg)
So can anyone let me know ..how to apply similar styles to our overlays (i mean to the lines in the overlay)..
Thanks in advance....
You have to subclass MKOverlayPathView and define your own drawing behaviour..
"If you subclass, you should override the createPath method and use
that method to build the appropriate path for the overlay. You can
invalidate this path as needed and force the path to be recreated
using whatever new data your subclass has obtained."

iOS storyboard: how to handle dynamic, floating content?

My App contains a news-section. I'm trying to create a View in my storyboard which will show an news item. A news contains of a title, a date, an image, and some text.
Content like this would be easy to display in HTML since it's content is floating, but as far as I understand iOS elements have a fixed size. I think a UILabel could be fine for title and date, UIImage for the image and a UITextView for the text. I think all elements should be inside a scroll view. The problem is, that the title for some news will fill one line but for other news multiple lines.
How can I handle a setup like this in a storyboard? A link to a guide, tutorial would be fine.
There are a lot of different ways you could approach this. Ultimately you have control of the size of any of these elements at runtime. From a UX point of view you'd probably want to keep at least some of the elements fixed in size (e.g., the image and the scrollview representing the total size of the news item). That would leave playing with the relative sizes of the title and the detail. If you want to show the entire title you can use the sizeWithFont method (or one of its variations) of the NSString class to calculate how much space to use for the title, and allocate space from the remaining amount to the detail as desired.
UPDATED: I might add that I intended this as a general strategy, not specifically aimed at the new iOS 5 Storyboard functionality.
UPDATED: Well, no tutorial but here are some general guidelines (at least about the way I might tackle it).
I'd set up a NewsItem class that encapsulates the elements that you describe here, similar to what I've laid out in the example image. How you expose NewsItem is something of a matter of taste; you might want to start with a UITableView as a container as it would make managing a NewsItem collection simpler. I'd subclass UITableViewCell as NewsItemTableViewCell, and generate a nominal layout for a NewsItem using Interface Builder. So in the example image, a news item would correspond to one table row.
The main part that you are asking about is identified by the red and blue frames in the image. Using the above approach, I would add logic in the NewsItemTableViewCell to calculate the extent of the title -- this assumes that the View (in this case, the NewsItemTableViewCell) has explicit knowledge of the Model (the underlying data for the news item) in the form of a reference (i.e, an instance of a NewsItem class
that contains the items you've described retrieved from a web service). If the width of the title is greater than the default width of the red frame, I'd adjust the height of the red frame (increasing it) at the same time decreasing the height of the blue frame. This kind of approach avoids having to include extra logic for differing cell heights within the table.
An alternative I often use is to have a Controller class mediate between the View and the Model. This can be useful when you have a requirement that your table contain different kinds of cells; it also makes tableview cell reuse much simpler. In this case, declare a base class. This might look something like:
#protocol GenericCellController
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
#optional
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
#end
and then:
#interface NewsItemTableCellController : NSObject<GenericCellController> {
NewsItem* newsitem;
}
You then set up a collection of table cell controller objects. This has the advantage that when you implement tableView:cellForRowAtIndexPath: in your tableview controller code, you only have to delegate to the cell controller object at the appropriate row. What's more, you can refer to it using a superclass designation; e.g., in a hypothetical NewsItemTableViewController class you might have:
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*) path {
NSObject<GenericCellController>* cellController = [cellControllerArray objectAtIndexPath:indexPath.row];
UITableViewCell* cell = [cellController tableView:tableView cellForRowAtIndexPath:indexPath];
return cell;
}
This approach allows you to have any class derived from NSObject in the collection and polymorphically use delegation to have the derived class handle the rendering logic. The derived Controller class has its own implementation of tableView:cellForRowAtIndexPath: with a corresponding UITableViewCell-derived subclass as described above.
If you decide to go this way, there are numerous examples of the basics of tableviews and their controllers that you can use as a preface to implementing these ideas.

Dropping Multiple Pins On Map given Co-ordinates Mapkit iPhone

I have a query regarding Dropping Multiple Pins On Map when Co-ordinates are given using Mapkit in iPhone.
Using Mapkit I am able to see the map at given latitude and longitude.
I am also able to drop a pin on one particular location.
However I have a array of lat and long and I want to show pins for them all.
How to achieve this?
I tried adding [mapView addAnnotation:pin] in loop, each time updating coordinates but it is not working.
Please help in this regard.
Do not using [mapView addAnnotation:pin] in loop,you can use this API: [mapView addAnnotations:pinsArray]. It works for me.
This is a screenshot of my App using multiple pins :
Nearby Dovebox http://dovebox.l99.com:81//Home_files/screenshots/Nearby.png
You can do so by a simple logic.
call annotation method in a loop from 0 to your [array count]; you must pass arguments to annotation method ie id, lat and long.
i have done it successfully..
make sure you have include pinmark nsobject class
Aamir