简体   繁体   中英

How can i increase zoom in Apple Mapkit with multiple annotations in iOS

I tried this code, here I increase MKCoordinateSpan to 200 - 200, but my application got crashed, can someone please help me.

 func setupMap(hotelListData:[HotelListData], cityLocation: 
                CLLocationCoordinate2D )
   {

    let coordinateRegion = MKCoordinateRegion(center: cityLocation, span: MKCoordinateSpan(latitudeDelta: 200, longitudeDelta: 200))
    mapView.setRegion(coordinateRegion, animated: true)

    //Set Multiple Annotation
    for data in hotelListData {
        let annotation = MKPointAnnotation()
        annotation.title = data.hotel?.name
        annotation.coordinate = CLLocationCoordinate2D(latitude: Double(data.hotel?.latitude ?? 0.0), longitude: Double(data.hotel?.longitude ?? 0.0))
        mapView.addAnnotation(annotation)
    }
}

Update:

I changed span from (latitudinalMeters & longitudinalMeters) and got the desired results :-

Now one can found here :

i)Set multiple Annotations to apple maps.

ii)Adjust Zoom with the desired location.

  func setupMap(hotelListData:[HotelListData], cityLocation: 
                 CLLocationCoordinate2D ){
    
    let coordinateRegion = MKCoordinateRegion(center: cityLocation, latitudinalMeters: CLLocationDistance(exactly: 15000)!, longitudinalMeters: CLLocationDistance(exactly: 15000)!)
    mapView.setRegion(coordinateRegion, animated: true)

    //Set Multiple Annotation
    for data in hotelListData {
        let annotation = MKPointAnnotation()
        annotation.title = data.hotel?.name
        annotation.coordinate = CLLocationCoordinate2D(latitude: Double(data.hotel?.latitude ?? 0.0), longitude: Double(data.hotel?.longitude ?? 0.0))
        mapView.addAnnotation(annotation)
    }
}

The longitudeDelta and latitudeDelta for a MKCoordinateSpan are measured in degrees. There are only 180 degrees of latitude from the north pole to the south pole, so using 200 for that parameter is not very sensible.

Since you want to show just the region of a city on the map, you can use this other initialiser that takes in distances in meters, if you know how big the cities that your app is handling usually are.

For example,

let coordinateRegion = MKCoordinateRegion(center: cityLocation, latitudinalMeters: 30000, longitudinalMeters: 30000)

If your cities have varying sizes, or you don't know how big they are, then another way is to calculate the range of latitudes and longitudes of the hotels, and use that to create an MKCoordinateSpan .

var minLat: CLLocationDegrees = 90
var maxLat: CLLocationDegrees = -90
var minLong: CLLocationDegrees = 180
var maxLong: CLLocationDegrees = -180
for data in hotelListData {
    // ... you annotation code ...

    guard let hotel = data.hotel else { continue }
    if hotel.latitude < minLat { minLat = hotel.latitude }
    if hotel.latitude > maxLat { maxLat = hotel.latitude }
    if hotel.longitude < minLong { minLong = hotel.longitude }
    if hotel.longitude > maxLong { maxLong = hotel.longitude }
}
let latRange = max(0.01, maxLat - minLat) // if the range is too small, make it at least 0.01
let longRange = max(0.01, maxLong - minLong)
let coordinateRegion = MKCoordinateRegion(
                           center: cityLocation,
                           span: MKCoordinateSpan(latitudeDelta: latRange, longitudeDelta: longRange)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM