2

I am using the code below to retrieve data from MySQL to show multiple locations on MKMapView using Swift.

The data and locations shows on the map, but what I could not figure out is how to adjust the zoom to cover all locations in that area.

 func parseJSON(_ data:Data) {
        var jsonResult = NSArray()

        do {
            jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
        } catch let error as NSError {
            print(error)
        }

        var jsonElement = NSDictionary()
        let locations = NSMutableArray()

        for i in 0 ..< jsonResult.count
        {
            jsonElement = jsonResult[i] as! NSDictionary

            let location = LocationModel()

            //the following insures none of the JsonElement values are nil through optional binding
            if let evIdL = jsonElement["id"] as? String,
               let evUserNameL = jsonElement["username"] as? String,
               let evNotikindL = jsonElement["notikind"] as? String,
               let evLatiL = jsonElement["lati"] as? String,
               let evLongiL = jsonElement["longi"] as? String,
               let evLocatL = jsonElement["locat"] as? String,
               let evTimedateL = jsonElement["timedate"] as? String,
               let evDistanceL = jsonElement["distance"] as? String
            {
                location.evId = evIdL
                location.evUsername = evUserNameL
                location.evNotikind = evNotikindL
                location.evLati = evLatiL
                location.evLongi = evLongiL
                location.evLocat = evDistanceL
                location.evTimedate = evTimedateL
                location.evDisatnce = evDistanceL
                location.evLocat = evLocatL

                // the code to show locations
                let latiCon = (location.evLati as NSString).doubleValue
                let longiCon = (location.evLongi as NSString).doubleValue

                  let annotations = locations.map { location -> MKAnnotation in
                     let annotation = MKPointAnnotation()
                     annotation.title = evNotikindL
                     annotation.coordinate = CLLocationCoordinate2D(latitude:latiCon, longitude: longiCon)
                     return annotation
                }

                self.map.showAnnotations(annotations, animated: true)
                self.map.addAnnotations(annotations)
            }

            locations.add(location)
        }

        DispatchQueue.main.async(execute: { () -> Void in
            self.itemsDownloaded(items: locations)
        })
    }

I am using PHP file to connect with MySQL, as I said the code working and showing the locations but the zoom focus on one location only.

Ahmed
  • 21
  • 4
  • look at this [https://stackoverflow.com/questions/4680649/zooming-mkmapview-to-fit-annotation-pins] – user832 Nov 27 '18 at 12:56
  • Possible duplicate of [Zooming MKMapView to fit annotation pins?](https://stackoverflow.com/questions/4680649/zooming-mkmapview-to-fit-annotation-pins) – Scriptable Nov 27 '18 at 12:58
  • unrelated, but have you considered using `guard` instead of`if let` block? It also seems pointless to do `locations.add(location)` after the `if let` – thedp Nov 27 '18 at 14:36

1 Answers1

2

You can try

DispatchQueue.main.async {
    self.map.addAnnotations(annotations)
    self.map.showAnnotations(annotations, animated: true)
    // make sure itemsDownloaded needs main ??
    self.itemsDownloaded(items: locations)

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • 1
    Good shout. [showAnnotations](https://developer.apple.com/documentation/mapkit/mkmapview/1452309-showannotations) - Sets the visible region so that the map displays the specified annotations. – Scriptable Nov 27 '18 at 12:59