-2

I'm trying to show a map with two markers with longitudes and latitudes that are retrieved from API.

the function that gets the longitudes and latitudes is called in Viewdidload

 override func viewDidLoad() {
    super.viewDidLoad()

    getCurrent()

also the code to show the map with markers in viewdidload so my full code looks something like this:

var longFamily  = ""
let latFamily = ""
var latShop = "" 
var longShop = "" 


override func viewDidLoad() {
    super.viewDidLoad()

    getCurrent()
    let coordinate₀ = CLLocation(latitude: CLLocationDegrees(Int(latFamily)!), longitude: CLLocationDegrees(Int(longFamily)!))
    let coordinate₁ = CLLocation(latitude: (Int(latFamily)!, longitude: (Int(longFamily)!))
    let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters
    let floatDistance = Float(distanceInMeters)


    // get two markers with shop and client locations



    map.delegate = self

    // 2.
    let sourceLocation = CLLocationCoordinate2D(latitude: (Int(latFamily)!, longitude: (Int(longFamily)!)
    let destinationLocation = CLLocationCoordinate2D(latitude: (Int(latShop)!, longitude: (Int(latShop)!)

I know there is something I have to do to get the data before the map loads but not sure where. I'd appreciate your help

GetCurrent function calls API :

 Alamofire.request(url!, method: .get, parameters: param,encoding: URLEncoding.default, headers: headers).responseJSON { response in



        if let value: AnyObject = response.result.value as AnyObject? {
            //Handle the results as JSON


            let data = JSON(value)
            self.LongShop = data["shopLong"] 
     // this is for family locations too  
AlmoDev
  • 969
  • 2
  • 18
  • 46
  • 1
    So you basically want to load the coordinates from an API (not MapKit), before the map loads so you can display them? – mcjcloud May 15 '17 at 19:40
  • @mcjcloud yes the API will give me the coordinates and the map is (MapKit) is in the view – AlmoDev May 15 '17 at 19:41
  • 1
    IMO this is just a design problem. Design your app to show a loading screen while you're waiting on the API call to return, then in the completion handler when you get the successful call back with the geo-coordinates, present a new VC with the map. Also you probably shouldn't use integers for latitude and longitude. – Pierce May 15 '17 at 20:22
  • thanks I solved the issue with the loading. now , as you just mention , I have an issue with latitude and longitude because Int doesn't put the location on the right places. What is the best conversion ? Or how to convert my string from API to valid latitude and longitude – AlmoDev May 15 '17 at 20:36

1 Answers1

0

If you want to have the coordinates before the map loads, you want to make the API call before the view is loaded (before viewDidLoad is called). You could try viewWillAppear, but depending on the response time from the API, this may be unreliable. I would suggest making the call to the API as soon as you have the data to do so. Get param from the user or desired source before hand (possibly in a different view), and pass it to the map view in the prepareForSegue function:

  1. Add a global variable to the map view (MapViewController.swift for example)

    class MapViewController {
        var param: Any?    // replace Any with the desired data type
        /* rest of MapView code */
    }
    
  2. Make the API call and pass in the previous View Controller and pass it to the MapViewController

    class PreviousViewController {
        var param: Any?    // replace Any with the desired data type
    
        override func viewDidLoad() {
            super.viewDidLoad()
            getFromAPI()    // getFromAPI being a function that makes the API call and assigns the param variable
        }
    
        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let dest = segue.destination as? MapViewController {
                dest.param = self.param
            }
        }
    }
    

On top of this, you can add features in which the MapViewController segue cannot be performed until param is not nil

mcjcloud
  • 351
  • 2
  • 11