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.

MGLPointAnnotation's do not appear when added asynchronously #2693

Closed

Description

@Jtango18

Utilising the example for drawing a GeoJSON shown on the MapBox maples page (https://www.mapbox.com/ios-sdk/examples/line-geojson/) I wanted to add functionality to add an annotation at the start and end of the line. I've discovered that adding annotations through asynchronous background->foreground dispatch calls causes the annotations not to be rendered until the map is zoomed and thus redrawn

Reproduction code below

    //
    //  ViewController.swift
    //  Mapbox Test
    //
    //  Created by JT on 19/10/2015.
    //  Copyright © 2015 JT. All rights reserved.
    //

    import UIKit
    import Mapbox

    class ViewController: UIViewController, MGLMapViewDelegate {


        var mapView: MGLMapView!

        override func viewDidLoad() {
            super.viewDidLoad()

            mapView = MGLMapView(frame: view.bounds)
            mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
            view.addSubview(self.mapView)

            mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: 45.5076, longitude: -122.6736),
                zoomLevel: 11, animated: false)

            mapView.delegate = self

            calculateMarkerPosition();
        }

        func calculateMarkerPosition() {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {


                //Do very computationally expensive things here to come up with some coordinates, 
                //but for repo purposes just fake it
                let annotationCoordinates = CLLocationCoordinate2DMake(45.52214, -122.63748);


                // Add the annotation on the main thread
                dispatch_async(dispatch_get_main_queue(), {
                    // Unowned reference to self to prevent retain cycle
                    [unowned self] in

                    let myAnnotation = MGLPointAnnotation();
                    myAnnotation.coordinate = annotationCoordinates;
                    myAnnotation.title = "Hello";
                    self.mapView.addAnnotation(myAnnotation);

                    })    
            })
        }

        func mapView(mapView: MGLMapView, alphaForShapeAnnotation annotation: MGLShape) -> CGFloat {
            // Set the alpha for all shape annotations to 1 (full opacity)
            return 1
        }

        func mapView(mapView: MGLMapView, lineWidthForPolylineAnnotation annotation: MGLPolyline) -> CGFloat {
            // Set the line width for polyline annotations
            return 2.5
        }

        func mapView(mapView: MGLMapView, strokeColorForShapeAnnotation annotation: MGLShape) -> UIColor {
            return UIColor.redColor()
        }

        func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
            return true
        }

        func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
            return nil;
        }
    }

Activity

  1. friedbunny commented on Oct 21, 2015

    @friedbunny
    Contributor

    This looks like an issue that was fixed in #2289 — can you confirm that you're using >=v2.1.0, @Jtango18? The code here works as expected for me.

  2. Jtango18 commented on Oct 21, 2015

    @Jtango18
    Author

    I'm using 2.1.2 as pulled down using Cocoapods. Perhaps there's an issue with pod. I'll have a go at using the latest binary/src and see if the issue is resolved for me.

  3. added
    iOS
    on Oct 22, 2015
  4. Jtango18 commented on Oct 22, 2015

    @Jtango18
    Author

    Just created a brand new project and added MapBox via Binary linking method. Same problem.

    Before Zooming
    simulator screen shot 22 oct 2015 12 46 24 pm

    After Zooming
    simulator screen shot 22 oct 2015 12 46 34 pm

  5. friedbunny commented on Oct 22, 2015

    @friedbunny
    Contributor

    It appears my local test project was using a newer version than 2.1.2, which indicates this has been fixed in master by some aspect of the recent annotation refactoring.

    Until the 2.2.0 release, the solution here is to load annotations only after the map has finished loading itself, which, coincidentally, is also difficult to wrangle until 2.2.0. This is a sloppy hack, but...

    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
    dispatch_after(delayTime, dispatch_get_main_queue()) {
        self.calculateMarkerPosition()
    }
  6. Jtango18 commented on Oct 22, 2015

    @Jtango18
    Author

    Thanks for all the help. I've been looking at MapBox as a crop in replacement for MapKit. Might stick with MapKit until 2.2.0

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

    bugiOS

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions