Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Antoine Streiff
4,649 PointsMapbox and POI in a dictionnary nested in an array
I'm dealing with a mapbox view (MGLMapView) with the Mapbox framework.
I'm trying to populate a map with POIs. I've stored the GPS coordinates in an array. However, I don't really how to, well, to put it in my own words, "create a constant for each value of the dictionary".
So far I've done a for loop :
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let dicoInterest = (dictionnaireMap["GPS"]!["interest"] as! NSArray)
let pointDepart = MGLPointAnnotation()
let latitudeDepart = (dictionnaireMap["GPS"]!["latitude"] as! Double)
let longitudeDepart = (dictionnaireMap["GPS"]!["longitude"] as! Double)
let zoom = (dictionnaireMap["GPS"]!["zoom"] as! Double)
pointDepart.coordinate = CLLocationCoordinate2D(latitude: latitudeDepart, longitude: longitudeDepart)
self.title = (dictionnaireMap["titre"] as! String)
mapViewOutlet.setCenterCoordinate(CLLocationCoordinate2D(latitude: latitudeDepart, longitude: longitudeDepart), zoomLevel: zoom, animated: false)
mapViewOutlet.addAnnotation(pointDepart)
for arrayInterest in dicoInterest {
let point = MGLPointAnnotation()
var latitudePoint : Double
var longitudePoint : Double
latitudePoint = (arrayInterest["latitude"] as! Double)
longitudePoint = (arrayInterest["longitude"] as! Double)
point.coordinate = CLLocationCoordinate2D(latitude: latitudePoint, longitude: longitudePoint)
print("latitude : \(latitudePoint)")
print("longitude : \(longitudePoint)")
}
}
The coordinates print well, but no point in view.
latitude : -38.96459
longitude : 146.35114
latitude : -38.96458
longitude : 146.35113
Any idea ?
2 Answers
Alex Millius
iOS Development Techdegree Student 5,468 PointsHi,
Maybe you could create a variable array of MGLPointAnnotation outside of your loop.
And each time you create your point, affect it to your array of MGLPointAnnotation.
Something like:
var myArrayOfMGLPointAnnotation = [MGLPointAnnotation]
for arrayInterest in dicoInterest {
let point = MGLPointAnnotation()
var latitudePoint : Double
var longitudePoint : Double
latitudePoint = (arrayInterest["latitude"] as! Double)
longitudePoint = (arrayInterest["longitude"] as! Double)
point.coordinate = CLLocationCoordinate2D(latitude: latitudePoint, longitude: longitudePoint)
myArrayOfMGLPointAnnotation.append(point)
print("latitude : \(latitudePoint)")
print("longitude : \(longitudePoint)")
}
Then you can iterate from myArrayOfMGLPointAnnotation to populate your map
Antoine Streiff
4,649 PointsGood idea!