I have created the following method for determining the view for my annotations in a map view.
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
MKPinAnnotationView *pin;
if ([annotation isKindOfClass:[AnnotationsWithIndices class]])
{
int i = [(AnnotationsWithIndices *)annotation index];
if (i > currentCheckpointIndex )
{
pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:#"unvisited"];
if (!pin)
{
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"unvisited"];
}
[pin setPinColor:MKPinAnnotationColorRed];
}
if (i == currentCheckpointIndex)
{
pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:#"current"];
if (!pin)
{
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
}
[pin setPinColor:MKPinAnnotationColorGreen];
}
if (i < currentCheckpointIndex)
{
pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:#"visited"];
if (!pin)
{
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"visited"];
}
[pin setPinColor:MKPinAnnotationColorPurple];
}
[pin setAnimatesDrop:YES];
return [pin autorelease];
}
else
return nil;
}
The idea is that I want the different annotation views (pins) be of different colors depending on whether the user has visited them to signal which annotation to visit next.
This code works fine, but I have a few questions about it which I hope someone can answer.
Firstly, the MKPinAnnotations are dequeued from a map view and reused whenever possible. In the line doing so (which I have found in multiple blogs and forums)
pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:#"unvisited"];
I understand that the return value of the dequeueReusableAnnotationViewWithIdentifier: is an instance of MKAnnotationView, whereas pin is a pointer to an instance of MKPinAnnotationView (which is a subclass o MKAnnotationView). This, I guess, is why there is a 'cast' seemingly going on with the prefix (MKPinAnnotationView *) in front of the method call. Is this really a cast and in this case, is it not dangerous since the MKPinAnnotationView contains more instance variables (than MKAnnotationView) and hence more space in memory?
I have tried to find some information about this, but nowhere I have not found anyone explaining this in particular.
Furthermore, the pointer annotation is either of class MKUserLocation or of my own custom class AnnotationsWithIndices conforming to the MKAnnotation protocol. Now, in order to determine which color the annotation view should have I have added an instance variable called index into the AnnotationsWithIndices class. Now, when I call the getter for the index I write
int i = [(AnnotationsWithIndices *)annotation index];
Now, I basically have the same question about this. Is there a cast going on here or is it just to let the compiler know that it is ok to send the message index to annotation? I guess that the compiler expects annotation to be an id whereas it really is a pointer to an instance of AnnotationsWithIndices.
Of course I know that this is the case since the annotation will be of my custom class, and I also check this explicitly to be sure. Is the (AnnotationsWithIndices) there only to signal to the compiler that this is OK?
I have also tried to find information about this without luck.
I am very thankful for any answers on this.
Related
i Am using following code
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([[annotation title] isEqualToString:#"Current Location"] )
{
MKAnnotationView *anView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentPin"];
anView.image = [UIImage imageNamed:#"pin_green.png"];
anView.canShowCallout = true;
anView.enabled = true;
return anView;
}
The issue is, it randomly disappears and appears again. Giving a very bad user experience. Any way to fix this?
There are several suspect things about this code:
You're not using dequeue, as someone has pointed out. In particular, the problem here is that you are making a new view every single time, rather than checking to see whether a new view needs making.
You are forgetting the key step, namely, to associate the view with the annotation.
Here is the canonical structure of a simple viewForAnnotation: implementation where we supply our own view:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView* v = nil;
if ([annotation.title isEqualToString:#"Current Location"]) {
static NSString* ident = #"greenPin";
v = [mapView dequeueReusableAnnotationViewWithIdentifier:ident];
if (v == nil) {
v = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:ident];
v.image = [UIImage imageNamed:#"pin_green.png"];
v.canShowCallout = YES;
}
v.annotation = annotation;
}
return v;
}
Since that code works for me, I'd suggest you start with it and tweak it as necessary.
By the way, you do NOT need this method just to get a green pin! You do know that, right? iOS will give you a green pin (MKPinAnnotationColorGreen).
You should use MKMapView's dequeueReusableAnnotationViewWithIdentifier: and see if you get a view back before creating a new one with initWithAnnotation:reuseIdentifier::
MKAnnotationView *anView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"currentPin"];
if (!anView) {
anView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentPin"];
anView.image = [UIImage imageNamed:#"pin_green.png"];
anView.canShowCallout = true;
anView.enabled = true;
}
return anView;
That said, I'm not entirely sure this is the cause of your problem.
I am adding a custom annotation along with the user's current location default bubble annotation but the user location annotation is changing to the other custom location after sometime when not in focus on mapview.
My viewForAnnotation method is :
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
NSString* annotationidentifier = #"customview";
CustomAnnotationView *customannotationview = (CustomAnnotationView*) [self.mapview dequeueReusableAnnotationViewWithIdentifier:annotationidentifier];
if([annotation isKindOfClass:[MKUserLocation class]])
{
customannotationview = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation reuseIdentifier:annotationidentifier annotationviewimage:[UIImage imageNamed:#"location pin28.png"]];
customannotationview.canShowCallout = YES;
return customannotationview;
}
else if(![annotation isKindOfClass:[MKUserLocation class]] && annotation != _mapview.userLocation && (annotation.coordinate.latitude != _locationmanager.location.coordinate.latitude && annotation.coordinate.longitude != _locationmanager.location.coordinate.longitude))
{
customannotationview = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation reuseIdentifier:annotationidentifier annotationviewimage:[UIImage imageNamed:#"image1.png"]];
return customannotationview;
}
return customannotationview;
}
I have put conditions in the custom annotation but still after some time if userlocation is out of focus for somtime it changes to image1.png
I believe your issue is the fact that both your user annotation and location annotation use the same identifier. What happens is, when you scroll the map around, your annotations get reused (the same way a table would reuse cells), and eventually, one of your location annotations is used for your user annotation.
Try giving the annotations different identifiers, or, if you have a small number of locations, remove the reuse code, that should fix it.
- (void) setTheMap
{
myMap.mapType = MKMapTypeStandard;
[myMap setDelegate:self];
CLLocationCoordinate2D coord = {latitude:myValue , longitude:myValue };
MKCoordinateSpan span = {latitudeDelta:0.05f , longitudeDelta: 0.05f};
MKCoordinateRegion region = {coord , span};
[myMap setRegion:region];
[self.view addSubview:myMap];
PantryAnnotation *dis = [[PantryAnnotation alloc] init];
dis.coordinate = region.center;
dis.title = row.panName;
dis.subTitle = row.geo_lat;
[myMap addAnnotation:dis];
}
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id<MKAnnotation>)annotation
{ MKPinAnnotationView *pview = nil;
if (annotation != myMap.userLocation)
{
static NSString *defalt = #"Volunteer";
pview = (MKPinAnnotationView *)[myMap dequeueReusableAnnotationViewWithIdentifier:defalt];
if ( pview == nil )
pview = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defalt] autorelease];
pview.pinColor = MKPinAnnotationColorRed;
pview.canShowCallout = YES;
pview.animatesDrop = YES;
}
else
{
[myMap.userLocation setTitle:#"I am Here"];
}
return pview;
}
The above gives me map view with annotation pin. I have to write this code in more than 8 files. Can any one tell me how can I centralized this code ? I mean to create only one class and by object of that class I can add the map in any other view.
Thanks..
Since the rest of your code is not clear, it is hard to give advice, but you may try to analyze it first;
what is common in these 8 code parts, and what is different?
Then you should figure out if you need to create and hold references to any local variables, lets say your map objects, or something like that.
If so, then subclassing would be the first choice to look at,
if not then an Obj-C category would probably do (http://macdevelopertips.com/objective-c/objective-c-categories.html). P.S: you cannot add member fields with this, only behavior (methods) are allowed.
If you need only objects with little to none behavior other than these, then you may make them plain objects holding the parameters only.
Or even you may use static methods like ( +(void) blahblah; ) just to be short, but dont do it unless you are sure that the behavior is generic in nature...
Well, a soup of advice here, if you provide more info, i can write in one of these directions in more details
good luck!
I'm new to programming and obj-c and currently working on a map based iphone app which will locate different sport facilities. I want each pin to show a different overlay, for instance a picture overlay will do fine, I'll add pics into the resource folder and I want each pin to reveal a different picture by clicking it. My example only includes one pin with its cordinates.
Now, I have now clue where to start, hope u can help me out!
thanks in advance :)
Here's my code:
[mapView addAnnotation:[MapLocation mapLocationtWithTitle:#"Beckomberga Sim och Sporthall" subtitle:#"Söderberga Allé 80" andCoordinate:(CLLocationCoordinate2D){ 59.35817, 17.89740 }]];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MapLocation class]])
{
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"PinAnnotation"];
pin.canShowCallout = YES;
pin.animatesDrop = NO;
pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.pinColor = MKPinAnnotationColorRed;
pin.pinColor = MKPinAnnotationColorGreen;
[pin autorelease];
return pin;
}
return nil;
}
Viktor,
Here is what you need to do:
Create a subobject of MKAnnotation that will represent your locations, SportsFacilityLocation
That object will contain title, subtitle, and location fields. This is to conform to the MKAnnotation protocol
Next you need an object that will be a subclass of MKAnnotationView, SportsFacilityMapView.
Finally in your viewForAnnotation delegate method you need to check each annotation type
and from that annotation type you will determine to return your SportsFacilityMapView
Check the docs on how to implement the Annotation and then AnnotationView. I have some example code at home that I will post later if you need it. Good Luck!
This is my -mapView:viewForAnnotation method which drops pins when i create annotation views. But when i set mapView.showsUserLocation = YES; in -viewDidLoad, i get a pin dropped on Infinite Loop (expected - in simulator) and not the normal blue dot.
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKAnnotationView *anno = nil;
//create a pin annotation view
MKPinAnnotationView *pin=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"]autorelease];
[pin setPinColor:MKPinAnnotationColorRed];
pin.animatesDrop=YES;
pin.canShowCallout = YES;
pin.calloutOffset = CGPointMake(-5, 5);
anno = pin;
return anno;
}
How can i get it to drop pins and show the blue dot as well?
Thanks
Really simple to fix, although unsure if this is the correct way to do it...
if (annotation == mapView.userLocation){
return nil; //default to blue dot
}
Similar to the other answer, here's something close:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSString *annotationType = [NSString stringWithCString:object_getClassName(annotation)];
if ([annotationType compare:#"NSKVONotifying_MKUserLocation"] == 0)
return nil;
...
}
Of course, use something like this at your own risk. It could stop working tomorrow if Apple decided to change that name.
Often you use your own class of annotation to look up information related to the annotation. In that case, to only handle your own annotations, use something like
if ([annotation isKindOfClass:[MapLocation class]]) {}