Map annotation swapping from displaying image to default pin randomly - ios

When the map is tested through simulator I notice that some annotations display the default pin instead of the customized image. When I go back to the apps Home menu and enter the mapview again the same thing happens with different annotations.
This code present 4 annotations out of 20 that I have which all uses the same code.
-(void)viewDidLoad {
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
[mapView setDelegate:self];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion test1 = { {0.0, 0.0} , {0.0, 0.0} };
test1.center.latitude = 55.705609;
test1.center.longitude = 13.195707;
[mapView setRegion:test1 animated:YES];
testmap *ann2 = [[testmap alloc] init];
ann2.title = #"test1";
ann2.subtitle = #"Klicka här för mer info";
ann2.coordinate = test1.center;
ann2.name = #"test1";
[mapView addAnnotation:ann2];
MKCoordinateRegion test2 = { {0.0, 0.0} , {0.0, 0.0} }; //ändra ner
test2.center.latitude = 55.710113;
test2.center.longitude = 13.213500;
[mapView setRegion:test2 animated:YES];
testmap *ann3 = [[testmap alloc] init];
ann3.title = #"test2";
ann3.subtitle = #"Klicka här för mer info";
ann3.coordinate = test2.center;
ann3.name = #"test2";
[mapView addAnnotation:ann3];
MKCoordinateRegion test3 = { {0.0, 0.0} , {0.0, 0.0} };
test3.center.latitude = 55.708981;
test3.center.longitude = 13.197266;
[mapView setRegion:test3 animated:YES];
testmap *ann4 = [[testmap alloc] init];
ann4.title = #"test3";
ann4.subtitle = #"Klicka här för mer info";
ann4.coordinate = test3.center;
ann4.name = #"test3";
[mapView addAnnotation:ann4];
MKCoordinateRegion test4 = { {0.0, 0.0} , {0.0, 0.0} }; //ändra ner
test4.center.latitude = 55.705170;
test4.center.longitude = 13.191277;
[mapView setRegion:test4 animated:YES];
testmap *ann5 = [[testmap alloc] init];
ann5.title = #"test4";
ann5.subtitle = #"Klicka här för mer info";
ann5.coordinate = test4.center;
ann5.name = #"test4";
[mapView addAnnotation:ann5];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return nil;
else
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier]; annotationView.canShowCallout = YES;
NSString* thisModelName = ((testmap*)annotation).name;
if ([thisModelName isEqualToString:#"test1"]) {
annotationView.image = [UIImage imageNamed:#"test1"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
annotationView.highlighted = YES;
return annotationView;
}
else if ([thisModelName isEqualToString:#"test2"])
{
annotationView.image = [UIImage imageNamed:#"test2"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
annotationView.highlighted = YES;
return annotationView;
}
else if ([thisModelName isEqualToString:#"test3"])
{
annotationView.image = [UIImage imageNamed:#"test3"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
annotationView.highlighted = YES;
return annotationView;
}
else if ([thisModelName isEqualToString:#"test4"])
{
annotationView.image = [UIImage imageNamed:#"test4"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
annotationView.highlighted = YES;
return annotationView;
}
}
return nil;
}

You're getting default pins "randomly" because of this code:
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (annotationView)
return nil;
What this code says is if the dequeueReusableAnnotationViewWithIdentifier: returns a view (ie. if annotationView is not nil), return nil from the delegate method.
When you return nil from the viewForAnnotation delegate method, the map view does the following:
If the annotation is of type MKUserLocation, it will display the default blue dot.
If the annotation is not an MKUserLocation, it will display the default red pin.
What you probably meant was:
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (annotationView)
return annotationView;
However, this will create a new problem because if the dequeue returns a view (a view that was previously used for a different annotation), the image of the dequeued view might not match the image that should be used for the current annotation.
So what will happen is annotation images will switch around "randomly" as you zoom and pan the map.
The proper approach in viewForAnnotation is to:
Get a reference to a view using dequeueReusableAnnotationViewWithIdentifier: and if that returns nil, create a new view. Set view properties that are common to all annotations only when creating a new view.
Using the view reference obtained in step 1, set view properties that are specific to the current annotation. If a dequeued view is being used, update its annotation property as well.
Example:
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
if (! [annotation isKindOfClass:[testmap class]])
{
//If annotation is NOT a "testmap" (eg. MKUserLocation),
//return nil so map view displays default view for it...
return nil;
}
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (! annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
//set view properties common to all annotations...
annotationView.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Do not use addTarget with a custom method for callout accessories.
//Instead, use the calloutAccessoryControlTapped delegate method shown below.
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.draggable = YES;
}
else {
//reusing a view, update its annotation to current...
annotationView.annotation = annotation;
}
//AFTER we have a view reference, set annotation-specific properties...
NSString* thisModelName = ((testmap*)annotation).name;
if ([thisModelName isEqualToString:#"test1"]) {
annotationView.image = [UIImage imageNamed:#"test1"];
}
else if ([thisModelName isEqualToString:#"test2"]) {
annotationView.image = [UIImage imageNamed:#"test2"];
}
else if ([thisModelName isEqualToString:#"test3"]) {
annotationView.image = [UIImage imageNamed:#"test3"];
}
else if ([thisModelName isEqualToString:#"test4"]) {
annotationView.image = [UIImage imageNamed:#"test4"];
}
/*
If image name will always be same as `name` property then
can use single line instead of if/else statements:
annotationView.image = [UIImage imageNamed:thisModelName];
*/
return annotationView;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if ([view.annotation isKindOfClass:[testmap class]]) {
testmap *tm = (testmap *)view.annotation;
NSLog(#"button for '%#' tapped", tm.name);
}
}

Related

How can i change mapkit annotation detail view background color?

Hi am trying to change the annotation detail view(show in map image) into a custom design like the second image.
How can i change that background image and all?
my current code is
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
mapView.showsUserLocation = YES;
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *identifier = #"myAnnotation";
MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[_MAPVIEW dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.pinColor = MKPinAnnotationColorPurple;
annotationView.canShowCallout = YES;
}else {
annotationView.annotation = annotation;
}
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(showDetailView) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = infoButton;
UIImageView *imageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"pin.png"]];
annotationView.leftCalloutAccessoryView=imageV;
return annotationView;
}
current annotation detail view design(when i click on pin then i can see the annotation detail view )
but i want something like this design
please help me.

MKMapView doesn't show popup on pin when tapped

I have a UIScrollView(gray) with a UIView (white) attached on. On the view I have attached an MKMapView with a pin inside.
For some unknown reason the pin doesn't show the popup when tapped.
Here some code:
UIView * whiteMapView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
whiteMapView.backgroundColor = [UIColor whiteColor];
whiteMapView.clipsToBounds = NO;
whiteMapView.userInteractionEnabled = YES;
[self.scrollView addSubview:whiteMapView];
x = 10.0f;
y = x;
w = CGRectGetWidth(whiteMapView.frame) - (x * 2);
h = w;
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(x, y, w, h)];
self.mapView.delegate = self;
self.mapView.scrollEnabled = NO;
[whiteMapView addSubview:self.mapView];
[self.mapView removeAnnotations:self.mapView.annotations];
Shop * shop = [[Shop alloc] init];
shop.name = self.userInfo[#"shopName"];
shop.shopDescription = self.userInfo[#"goods_description"];
shop.latitude = self.userInfo[#"lat"];
shop.longitude = self.userInfo[#"lon"];
MapPin * pin = [[MapPin alloc] initWithShop:shop];
[self.mapView addAnnotation:pin];
Here the delegate method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
else
{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Pin"];
pinView.userInteractionEnabled = YES;
pinView.image = [UIImage imageNamed:#"pin_mappa_red"];
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
disclosureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
disclosureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[disclosureButton addTarget:self action:#selector(didPressPin:) forControlEvents:UIControlEventTouchUpInside];
disclosureButton.tag = [[[(MapPin *)annotation shop] idshop] intValue];
pinView.rightCalloutAccessoryView = disclosureButton;
return pinView;
}
}
In another part of the app this code works perfectly.
Ideas ?
Try to change like this viewForAnnotation as follows
-(MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if(annotation)
{
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImageView *imageInMap = [[UIImageView alloc]initWithImage: [UIImage imageNamed:#"net.png"]];
[disclosureButton addTarget:self action:#selector(mapCallOutPressed) forControlEvents:UIControlEventTouchUpInside];
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[self.locationMapVIew dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.rightCalloutAccessoryView = disclosureButton;
pinView.leftCalloutAccessoryView = imageInMap;
pinView.leftCalloutAccessoryView.frame = CGRectMake(0, 0, 35, 35);
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else {
[self.locationMapVIew.userLocation setTitle:#"I am here"];
}
return pinView;
}
Check this method is call or not
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog("Pin called");
}
If this method is not called then there may be any problem for mapview or Annotation.

callout accessory not appearing

I am trying to add a generic callout accessory to my annotation area but its not showing up for some reason.
Here's the code:
- (void) setupMap:(PFObject*) venue {
...
OTNVenueAnnotation *annotation = [[OTNVenueAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude);
annotation.title = [[venue objectForKey:#"name"] uppercaseString];
annotation.venue = venue;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"identifier"];
customPinView.leftCalloutAccessoryView = rightButton;
[self.mapView addAnnotation: annotation];
}
Try to set the leftCalloutAccessoryView in the delegate method of the map view viewForAnnotation, for example:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *annView= (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:#"identifier"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.leftCalloutAccessoryView = rightButton;
annView.canShowCallout = YES;
return annView;
}

callout is not showing by default in map

I am trying to show callout on a pin by without clicking on the pin.In short, I want show the callout when the pins are getting placed on the map.
This is code I am using:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation // Method to handle all the annotations on map.
{
if([annotation isKindOfClass: [MKUserLocation class]])
return nil;
static NSString *annotationID = #"MyAnnotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;
if (!pinView) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"map_pin.png"];
//Setting Right call button
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Setting Left Call button
self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.favButton.frame = CGRectMake(0, 0, 23, 23);
self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.favButton setImage:[UIImage imageNamed:#"favourite.png"] forState:UIControlStateNormal];
pinView.leftCalloutAccessoryView = self.favButton;
[self.mapView selectAnnotation:annotation animated:YES];
}
return pinView;
}
Any help would be appreciable.
Try with This :
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
if ([currentAnnotation isEqual:annotationToSelect]) {
[mapView selectAnnotation:currentAnnotation animated:YES];
}
}
}
OR
EDIT :
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation // Method to handle all the annotations on map.
{
if([annotation isKindOfClass: [MKUserLocation class]])
return nil;
static NSString *annotationID = #"MyAnnotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;
if (!pinView) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"map_pin.png"];
//Setting Right call button
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Setting Left Call button
self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.favButton.frame = CGRectMake(0, 0, 23, 23);
self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.favButton setImage:[UIImage imageNamed:#"favourite.png"] forState:UIControlStateNormal];
pinView.leftCalloutAccessoryView = self.favButton;
**[self performSelector:#selector(selectAnnotation:) withObject:annotation afterDelay:0.5];**
}
return pinView;
}
- (void)selectAnnotation:(id < MKAnnotation >)annotation
{
[self.mapView selectAnnotation:annotation animated:NO];
}

Update annotation's subtitle

I'm struggling with setting the subtitle of an annotation in MapView, once it has been clicked.
.m:
MKCoordinateRegion England = { {0.0, 0.0} , {0.0, 0.0} };
England.center.latitude = 51.50063;
England.center.longitude = -0.124629;
England.span.longitudeDelta = 100.0f;
England.span.latitudeDelta = 100.0f;
[mapView setRegion:England animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"England";
ann1.subtitle = #"subtitle";
ann1.coordinate = England.center;
[mapView addAnnotation:ann1];
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation {
MKPinAnnotationView *MyPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"]; MyPin.pinColor = MKPinAnnotationColorPurple;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop = TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)view
{
id<MKAnnotation> selectedAnn = [mapView.selectedAnnotations objectAtIndex:0];
country_title = selectedAnn.title;
selectedAnn.subtitle = #"NEW SUBTITLE";
}
How do I set the subtitle once the annotation has been clicked? I wan't to do it that way, because I get some rows from mysql when it's clicked.. but I don't know how to update the subtitle for the specific annotation?
Thanks in advance! :)
id<MKAnnotation> selectedAnn = view.annotation;
[mapview removeAnnotation:selectedAnn];
.. Add Annotation with new subtitle
[mapview addAnnotation:newselectedAnn];
[newselectedAnn setSelected:Yes animated:No];
Hope this helps

Resources