Lohith Korupolu
Lohith Korupolu

Reputation: 1074

Swift mkmapview get zoom level, width or radius of visible map area from latitude longitude delta

I am in a confusion, on how to get a zoom level and radius of a visible area of the map (using mapkit mapview).

Here is what I am looking for (either of them or both, as needed) -

  1. Zoom level, is to see at what level of the map is being shown to the users and with that information, I want to display the custom pins in the visible area. If zoom level is high, I need to show the actual logos of some commerce stores as pins. If zoom level is low, I need to show colored dots instead of logos.

As of now, I am using let updatedRadius = (mapView.camera.altitude)/1000 to get altitude of the camera, and if the updatedRadius value is > 25.0, I am showing colored dots. Below 25.0, I show the logos. I am doing this in regionDidChanged Is this approach correct?

  1. Radius, is to send it as a parameter to my REST API to fetch the list of places within that radius. When user zooms out on the map, visible area increases and so the REST API needs bigger radius to return the places covered in that area.

Ultimately, what should happen is, whenever user zooms out, then the radius changes. I need to send this changed radius to my REST to get an updated list.

  1. What are latitude longtitude deltas, can we get radius/width of visible area using these values?

    let latitudeDeltaVal = mapView.region.span.latitudeDelta let longitudeDeltaVal = mapView.region.span.longitudeDelta

Can someone throw some light on what needs to be done please?

Upvotes: 2

Views: 4235

Answers (2)

J Kasparian
J Kasparian

Reputation: 537

To account for both landscape and portrait orientations, and/or situations where the map orientation is close to Northeast, Northwest, Southwest, Southeast, and to enclose the full screen up to the corners, one should consider both latitudeDelta and longitudeDelta:

func getRadius() -> Double{
    let centralLocation = CLLocation(latitude: mapView.region.center.latitude, longitude: mapView.region.center.longitude)
    let cornerOfMap = CLLocation(latitude: centralLocation.coordinate.latitude + mapView.region.span.latitudeDelta , longitude: centralLocation.coordinate.longitude + mapView.region.span.longitudeDelta)
    let radius = centralLocation.distance(from: cornerOfMap)
    return radius / 1000.0 // to convert radius to meters
}

Upvotes: 0

Aravind A R
Aravind A R

Reputation: 2714

Since you need to call the api when the region changes you need to calculate the radius in mapView's delegate function, RegionDidChange.

    func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        let centralLocation = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude:  mapView.centerCoordinate.longitude)
        self.centralLocationCoordinate = mapView.centerCoordinate

        print("Radius - \(self.getRadius(centralLocation))")

    }


    func getRadius(centralLocation: CLLocation) -> Double{
        let topCentralLat:Double = centralLocation.coordinate.latitude -  mapView.region.span.latitudeDelta/2
        let topCentralLocation = CLLocation(latitude: topCentralLat, longitude: centralLocation.coordinate.longitude)
        let radius = centralLocation.distanceFromLocation(topCentralLocation)
        return radius / 1000.0 // to convert radius to meters 
    }

Upvotes: 9

Related Questions