I am using swift 3. When I hit a specific string while going through a for loop of iterated geojson data (jsonObj[i]) I need it to call the right image file. I am able to return the right pass type and load my markers in the map correctly. I just can't seem to figure out how to get the app to load the right color per marker type.
I have tried a switch statement but with no luck. I have looked at the if statement documentation among many other stack overflow posts and tutorials.
var pass : String?
var brown = UIImage(named: "brown.png")
var purple = UIImage(named: "purple.png")
var green = UIImage(named: "green.png")
var image : UIImage?
This block of code below is in the for loop. So each time it goes through the data it reads the new data and loads that into a marker as a title and subtitle. With this code it reads the first marker "purple" and loads all points with the purple marker.
var pass = jsonObj[i]["properties"]["Pass"]
print("propertiesPassString: ", pass)
if pass == "pass1" {
print("Pass1!")
image = purple
}
else if pass == "pass2" {
print("Pass2")
image = brown
}
else if pass == "" {
print("Pass3")
image = green
}
Here is the func that loads the marker, not sure if this is needed but it applies the "image" variable in each iteration of the for loop. This is largely copy and pasted from mapbox ios sdk
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
// Always try to show a callout when an annotation is tapped.
return true
}
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
// Try to reuse the existing ‘point’ annotation image, if it exists.
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: "point")
if annotationImage == nil {
print("annotationImage")
image = image?.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: (image?.size.height)!/2, right: 0))
// Initialize the ‘point’ annotation image with the UIImage we just loaded.
annotationImage = MGLAnnotationImage(image: image!, reuseIdentifier: "point")
}
//image = nil
return annotationImage!
}
If you have any advice/suggestions for a newer programmer in swift 3 I would certainly appreciate it. I've been stuck on this for several days now. I am still learning how to utilize stack overflow as well so please let me know if there is a better way to go about posting this question, I am open to constructive critiques.
Aucun commentaire:
Enregistrer un commentaire