Map Annotations for GeoJSON using URL session not appearing

Hi All, I am creating my first app and am not a developer and am really stuck!! I used Ray W’s code from the Honolulu Art example ( https://www.raywenderlich.com/7738344-mapkit-tutorial-getting-started#toc-anchor-005)

The problem is that instead of using GeoJSON file in my Project like Ray did, I am calling a URL session to get the GeoJSON from a third party source then parsing it and adding fire icons to a map. It works fine if I manually create a single annotation with the fire data, but does not work when I get an array of fire locations in the URL session. In addition, I verified the URL session is returning data so it is just the annotations not getting added to the map. Here is my code from the ViewController.swift. Any advice would be GREATLY appreciated and I can provide more information if needed on the Map View I registered or the model! Thanks very much.

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
var locationManager:CLLocationManager!
var lat = Double()
var lon = Double()
var fires: [Fire] = []

override func viewDidLoad() {

    super.viewDidLoad()
 
    mapView.delegate = self
    mapView.register(FireMarkerView.self,forAnnotationViewWithReuseIdentifier:
        MKMapViewDefaultAnnotationViewReuseIdentifier)
      
    if let url = URL(string: "https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Active_Fires/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson") {
          
        URLSession.shared.dataTask(with: url) {data, response, error in
                if let data = data {
                   do {
                       let features = try MKGeoJSONDecoder().decode(data)
                           .compactMap { $0 as? MKGeoJSONFeature }
                         let validWorks = features.compactMap(Fire.init)
                       self.fires.append(contentsOf: validWorks)
                             }
                   catch let error {
                                print(error)
                               
                   }
                 }
             }.resume()
       }
    
    
  //This code works //* let fire = Fire(
    title: "Ford Fire",
    incidentShortDescription: "Hwy 35",
    incidentTypeCategory: "WF",
    coordinate: CLLocationCoordinate2D(latitude: 37.7993, longitude: -122.1947))
   
    mapView.addAnnotation(fire)*/
    
    mapView.addAnnotations(fires)
}
    
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    determineMyCurrentLocation()
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {

switch manager.authorizationStatus {
    case .authorizedAlways , .authorizedWhenInUse:
        mapView.showsUserLocation = true
        followUserLocation()
        locationManager.startUpdatingLocation()
        break
    case .notDetermined , .denied , .restricted:
        locationManager.requestWhenInUseAuthorization()
        break
    default:
        break
}

switch manager.accuracyAuthorization {
    case .fullAccuracy:
        break
    case .reducedAccuracy:
        break
    default:
        break
}

}
func followUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: 4000, longitudinalMeters: 4000)
mapView.setRegion(region, animated: true)
}
}

func determineMyCurrentLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    
    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
        //locationManager.startUpdatingHeading()
    }
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last else { return }
    
    let userLocation = locations.first! as CLLocation
    lat = userLocation.coordinate.latitude
    lon = userLocation.coordinate.longitude
    
    let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: 400000, longitudinalMeters: 400000)
    self.mapView.setRegion(region, animated: true)

    // Call stopUpdatingLocation() to stop listening for location updates,
    // other wise this function will be called every time when user location changes.
    // Need a solution for this.
            manager.stopUpdatingLocation()
    
    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
    print("Error \(error)")
}

}

This topic was automatically closed after 166 days. New replies are no longer allowed.