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

Replace MGLAnnotation image on didSelectAnnotation: #2210

Closed

Description

@hshah2811

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 
}

Activity

  1. added
    iOS
    on Sep 1, 2015
  2. incanus commented on Sep 1, 2015

    @incanus
    Contributor

    This will rely on #1688.

  3. incanus commented on Sep 1, 2015

    @incanus
    Contributor

    Refs #1980

  4. hshah2811 commented on Sep 8, 2015

    @hshah2811
    Author

    May i know when can we expect this fix?

  5. incanus commented on Sep 8, 2015

    @incanus
    Contributor

    @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.

  6. minikin commented on Oct 29, 2015

    @minikin

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

  7. RomainQuidet commented on Nov 26, 2015

    @RomainQuidet
    Contributor

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

  8. RomainQuidet commented on Nov 30, 2015

    @RomainQuidet
    Contributor

    Code is working, cleaning up and pull request coming soon

  9. RomainQuidet commented on Nov 30, 2015

    @RomainQuidet
    Contributor

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

  10. added this to the ios-v3.1.0 milestone on Jan 14, 2016
  11. friedbunny commented on Jan 14, 2016

    @friedbunny
    Contributor

    Fixed by #3146.

  12. minikin commented on Feb 22, 2016

    @minikin

    @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.

  13. RomainQuidet commented on Feb 23, 2016

    @RomainQuidet
    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"];
    }
  14. minikin commented on Feb 23, 2016

    @minikin

    @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.

  15. RomainQuidet commented on Feb 23, 2016

    @RomainQuidet
    Contributor

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

  16. minikin commented on Feb 23, 2016

    @minikin

    @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

  17. minikin commented on Feb 23, 2016

    @minikin

    @1ec5 maybe you can help me? Thanks!

  18. 1ec5 commented on Feb 23, 2016

    @1ec5
    Contributor

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

  19. alexagat commented on Mar 31, 2016

    @alexagat

    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?

  20. RomainQuidet commented on Mar 31, 2016

    @RomainQuidet
    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.

  21. Ramshandilya commented on May 25, 2016

    @Ramshandilya

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

  22. 1ec5 commented on May 25, 2016

    @1ec5
    Contributor

    @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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureiOS

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions