Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Replace MGLAnnotation image on didSelectAnnotation: #2210

Closed
hshah2811 opened this issue Aug 31, 2015 · 20 comments
Closed

Replace MGLAnnotation image on didSelectAnnotation: #2210

hshah2811 opened this issue Aug 31, 2015 · 20 comments
Labels
feature iOS Mapbox Maps SDK for iOS
Milestone

Comments

@hshah2811
Copy link

In Earlier Mapbox version 1.X, we do have RMMarker.replaceUIImage(Image) API to change the annotation image on marker/annotation selection.

But in new 2.0+ versions, i cann't see any API available on MGLAnnotation to replace the existing annotation image.

Requirement is: I want to change annotation/marker image when user taps/select on any annotation/marker in the map.

CODE:

Assigning non-selected image to all markers during map load.

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(LocationBasedObjectAnnotation *)annotation {
    UIImage * image = [UIImage imageNamed:@"nonSelectedImage"];
    NSString * reuseIdentifier = NSStringFromClass(annotation.class);
    return [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:reuseIdentifier];
}

Once user taps:

- (MGLAnnotiation*) mapView:(MGLMapView*)mapView didSelectAnnotation:(MGLAnnotation*)annotation
{
    //No API to change image of selected annotation 
}
@incanus incanus added iOS Mapbox Maps SDK for iOS feature labels Sep 1, 2015
@incanus
Copy link
Contributor

incanus commented Sep 1, 2015

This will rely on #1688.

@incanus
Copy link
Contributor

incanus commented Sep 1, 2015

Refs #1980

@hshah2811
Copy link
Author

May i know when can we expect this fix?

@incanus
Copy link
Contributor

incanus commented Sep 8, 2015

@hshah2811 That (#1688) is of a class of bug/enhancement we are trying to get into this week's release, but it may not quite make it. Stay tuned — on a short timeline.

@minikin
Copy link

minikin commented Oct 29, 2015

I also looking forward to fix this bug/enhancement. Thank you.

@RomainQuidet
Copy link
Contributor

FYI I'm working on it. Will send a pull request soon

@RomainQuidet
Copy link
Contributor

Code is working, cleaning up and pull request coming soon

@RomainQuidet
Copy link
Contributor

Pull request sent : (#3146)
Just get your annotationImage and update the image property

@friedbunny
Copy link
Contributor

Fixed by #3146.

@minikin
Copy link

minikin commented Feb 22, 2016

@RomainQuidet Could you give me an example how to change an image(MGLAnnotationImage) for selected annotation in didSelectAnnotation? I can't figure out ...
Thank you in advance.

@RomainQuidet
Copy link
Contributor

@minikin : I'm doing this on my sample

- (void)mapView:(MFMapView *)mapView didSelectAnnotation:(nonnull id<MGLAnnotation>)annotation
{
    // set selected annotation image
    MGLAnnotationImage *annoImage = [self.annotationImages objectForKey:annotation.title];
    annoImage.image = [UIImage imageNamed:@"marker-green"];
}

@minikin
Copy link

minikin commented Feb 23, 2016

@RomainQuidet Thank you very much!
Just one question: How do you define annotationImages ?
I do like this :
var annotationImages = [String:MGLAnnotationImage]() also tried var annotationImages = [String:AnuObject]() but I can't call objectForKey

I tried like that : annotationImages["\(annotation.title!!)"]?.image = UIImage(named: "selectedPin")! in didSelectAnnotation but in this case all annotations change images...

I would appreciate any advice. Thanks.

@RomainQuidet
Copy link
Contributor

@minikin MGLAnnotationImages are objects in the mapBox SDK with a designated initializer
+ (instancetype)annotationImageWithImage:(UIImage *)image reuseIdentifier:(NSString *)reuseIdentifier;
(in ObjC)

@minikin
Copy link

minikin commented Feb 23, 2016

@RomainQuidet Thank you.
If I understand you correctly I need to do like this:

   let annotationImage = self.mapView.dequeueReusableAnnotationImageWithIdentifier("nonSelectedImage")

       let annoImage = annotationImage?.valueForKey(annotation.title!!) as! MGLAnnotationImage

      annoImage.image = UIImage(named: "selectedPin")!

I have an error :

[< UIImage 0x7f91060f0cd0 > valueForUndefinedKey:]: this class is not key value coding-compliant for the key Antalia.

objc[54908]: pthread_rwlock_rdlock failed (11)

ps: annotation.title!!- looks weird

@minikin
Copy link

minikin commented Feb 23, 2016

@1ec5 maybe you can help me? Thanks!

@1ec5
Copy link
Contributor

1ec5 commented Feb 23, 2016

@minikin, please contact our Support staff, and we’d be happy to assist you further.

@alexagat
Copy link

I am still facing an issue trying to change an annotation image on the fly (without removing and adding all annotations).

I create a dictionary of annotation titles to annotations - [String : MGLAnnotationImage]
var annotationsTitlesAndImages: [String:MGLAnnotationImage] = [:]

I populate this dictionary with all my MGLAnnotationImages within the imageForAnnotation function

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
    let annotationImage = MGLAnnotationImage(image: UIImage(named: "map-pin")!, reuseIdentifier: "map-pin")
    annotationsTitlesAndImages[annotation.title!!] = annotationImage
    return annotationImage
}

I try and update the annotation's image in the didSelectAnnotation function

func mapView(mapView: MGLMapView, didSelectAnnotation annotation: MGLAnnotation) {
    annotationsTitlesAndImages[annotation.title!!]!.image = UIImage(named: "map-pin-active")!
}

With a breakpoint set on the annotationsTitlesAndImages[annotation.title!!]!.image line above, I see this code in the framework being run:

- (void)setImage:(UIImage *)image {
    _image = image;
    [self.delegate annotationImageNeedsRedisplay:self];
}

...but the image doesn't update.

Any insight?

@RomainQuidet
Copy link
Contributor

replacing the image property of annotationImage should do the job. But I've seen yesterday an error saying
{Map}[Sprite]: Can't change sprite dimensions for 'com.mapbox.sprites.4'

I'll check that.

@Ramshandilya
Copy link

@alexagat Were you able to find a way to change image of an annotation? (without removing & adding)

@1ec5
Copy link
Contributor

1ec5 commented May 25, 2016

@alexagat @Ramshandilya, if setting the MGLAnnotationImage’s image property doesn’t work following #3146, it definitely should work following #3835 (which no longer causes the annotation to be removed and readded). Please try it out in the v3.3.0 alphas and let us know if it isn’t working as expected. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature iOS Mapbox Maps SDK for iOS
Projects
None yet
Development

No branches or pull requests

8 participants