Simple Question: Seems I cannot find the tag attribute for MKPointAnnotation class. It return error;
MKPointAnnotation *annotation = [[HCIAnnotationViewController alloc]
initwithHouse:house];
The following returns error (Property tag not found ob object of type "MKPointAnnotation"
NSLog(#"%d",annotation.tag);
My question is, if Im not allowed to set the tag, How am I supposed to detect which annotation was clicked.
The other approaches I followed are
Setting tag for MkAnnotationView, However in this what I found out is that the last annotation when added doesn't immediately call for viewForAnnotation (Might be because I'm adding around 1000 MkPointAnnotations to a small map, so it only calls when it comes to view.).
Please tell me how to resolve this?
Since MKPointAnnotation is not a subclass of UIView, has not a property called tag. Bu you have the property coordinate. I assume you have different coordinates for all of your annotations. So you can detect which one.
You just need to compare the coordinates.
Related
I queried a set of coordinates for users around me from Parse(PFGeoPoint) and I have created annotations for said locations/coordinates. I will be updating the users location every 5 seconds and updating the location of the annotations, everything is working fine except for the fact that I'm having to remove and add annotations instead of them moving on their own similar to lyft, is there a way to do this in swift? please reply, thank you!!
Yes it is. As I can see from your tags, you are using MKPointAnnotation , so you an access its coordinate property and update it as you wish.
var coordinate: CLLocationCoordinate2D
So all you have to do is set it up like this:
annotation.coordinate = newCoordinate
For more information, please look at Accessing the Annotation’s Location.
I have added a button on my .xib file with which I want to delete the last annotation that has been added.
So on the Touch-Down action I have implemented this:
-(IBAction)DeleteAnnotation:(id)sender {
[mapview removeAnnotation:[mapview.annotations lastObject]];
}
and I have even tried it this way:
-(IBAction)DeleteAnnotation:(id)sender {
[self.mapview removeAnnotation:self.mapview.annotations.lastObject]];
}
where mapview is my MKMapView Outlet.
The problem I encounter with both ways is that I have to press this specific button quite a few times before an annotation is removed.
Furthermore, the annotations remove themselves in a quite random way.
Is there something I am doing wrong or is it a software and simulator matter?
The annotations property of MKMapView is not guaranteed to return the annotations in the same order that you added them.
Making the assumption that the annotations array property will return the annotations in the same order that you added them is most likely the reason for the "strange" behavior you see. Please see these related answers for some more details:
MKMapView annotations changing/losing order?
How to reorder MKMapView annotations array
To get the behavior you want (which I assume is simply "remove the last annotation that was added explicitly by my code"), here are three possible approaches (there may be others):
Simplest approach is to keep a reference in a strong property to the last annotation you add (update the reference when you call addAnnotation). When you want to remove the "last annotation added", pass that saved reference to removeAnnotation. For example:
//in the interface...
#property (nonatomic, strong) id<MKAnnotation> lastAnnotationAdded;
//in the implementation...
//when you add an annotation:
[mapview addAnnotation:someAnnotation];
self.lastAnnotationAdded = someAnnotation; //save the reference
//when you want to remove the "last annotation added":
if (self.lastAnnotationAdded != nil)
{
[mapview removeAnnotation:self.lastAnnotationAdded];
self.lastAnnotationAdded = nil;
}
Another option is to loop through the map view's annotations array and search for the "last" annotation (or whatever attribute you're interested in). Once you have a reference to the "last" one (which may not necessarily be the last object in the array), you can call removeAnnotation on it. This approach assumes you have some property in the annotation objects themselves that let you identify an annotation as the "last" one. This may not always be possible.
Another option is to keep your own array of annotations and add annotation objects to this array whenever you call addAnnotation. This is similar to keeping the single reference to just the "last annotation added" except you keep track of the entire list in an order you can rely on. To remove the "last" one, you would get lastObject from your array instead of the map view's (assuming you keep the array in that order). You have to make sure to keep your array in sync as you add/remove annotations from the map.
Not very well versed with iOS Development. I have a very stupid question to ask!! is there a difference between Placemarks and Annotations. Ive been through some Apple Developer documents. Somewhere it was mentioned Placemarks have areaOfInterest property. Im not sure if annotations have one too.
Apart from that how do Placemarks differ from Annotations??
Thanks in Advance!! Cheers
Annotations offer a way to highlight specific coordinates on the map and provide additional information about them. You can use annotations to call out specific addresses, points of interest, and other types of destinations. When displayed on a map, annotations typically have some sort of image to identify their location and may also have a callout bubble providing information and links to more content.
For Placemarks See This Link
http://ioscreator.com/display-placemarks-with-mapkit/
From the MKPlacemark class reference:
A placemark is also an annotation and conforms to the MKAnnotation protocol, whose properties and methods include the placemark coordinate and other information. Because they are annotations, you can add them directly to the map view.
MKPlacemark inherits from CLPlacemark, which has property areasOfInterest.
A look at the class references for both classes and the protocol should clear things up.
I am working on mapview project. I have question related to removing annotation from the mapView.
I have implemented the following code but it removes annotation randomly, not the first one!
[mapView removeAnnotation:[self.mapView.annotations objectAtIndex:0]];
self.mapView.annotations gives you an NSArray of all the annotations, but it does not promise to be in the same order each time nor in the order you added annotations. You will have to find another way to get a reference to the one you intend to delete.
I have came across a solution. Whomever concerns about this question's answer, there you go
I have created an NSMUtable array and add all objects into array then remove first element from the mapview
[mapView removeAnnotation:[annotationArray objectAtIndex:0]];
I have a custom annotation that sets its image based on the type of the annotation using the viewForAnnotation delegate method. Im only using 1 annotation that represents a car moving and want to change the image for when the car is detected to be moving and stopped. How could I go about this besides removing my annotation and re-adding it which causes a blink?
Wherever you detect that the car's state has changed, retrieve the annotation's current view using the MKMapView instance method viewForAnnotation:. This is not the same as the mapView:viewForAnnotation: delegate method.
After getting the current view for the annotation, you can modify its properties including image.
Also make sure the mapView:viewForAnnotation: delegate method has the same exact condition to set image based on the state of the car annotation. You may want to put the logic in a common method called from both places (where the state changes and the delegate method) so the code isn't duplicated.
For example, where the state changes, you might have:
//carAnnotation is your id<MKAnnotation> object
MKAnnotationView *av = [mapView viewForAnnotation:carAnnotation];
if (carAnnotation.isMoving)
av.image = [UIImage imageNamed:#"moving.png"];
else
av.image = [UIImage imageNamed:#"stopped.png"];
The if statement (or whatever logic you have to set image) is the part that should also be in the viewForAnnotation delegate method.