Search code examples
objective-crubymotion

How to set a initial zoom level for a map?


I am building an iOS app using Rubymotion. In this app I got a mapview. I can display this fine but I want to set an initial zoom level to the annotation area instead of a fully zoomed out view. How can I do this?

This is my code:

map = MKMapView.alloc.initWithFrame(CGRectMake(10, 10, (hash[:width]-20), (hash[:height]-20)))
    map.mapType = MKMapTypeStandard
    map.delegate = self
    map.showsUserLocation = true
    map.setCenterCoordinate(location, animated:true)
    map.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

Solution

  • You can use longitudeDelta and latitudeDelta fields of the MKCoordinateSpan structure:

    MKCoordinateSpan span; span.latitudeDelta = .001;
    span.longitudeDelta = .001;
    //the .001 here represents the actual height and width delta
    MKCoordinateRegion region;
    region.center = newLocation.coordinate; region.span = span;
    [mapView setRegion:region animated:TRUE];
    

    Of course you can store the span for example as default zoom level.

    Here's what docs say:

    "*The region displayed by the map is defined by the region property, which is a structure of type MKCoordinateRegion. The MKCoordinateRegion structure contains a member called center (of type CLLocationCoordinate2D) and another member called span (of type MKCoordinateSpan). The MKCoordinateSpan structure in turn contains two members: latitudeDelta and longitudeDelta (both of type CLLocationDegrees, which is a double) Both members define the amount of distance to display for the map:

    latitudeDelta — One degree of latitude is approximately 111 kilometers (69 miles).

    longitudeDelta — One degree of longitude spans a distance of approximately 111 kilometers (69 miles) at the equator but shrinks to 0 kilometers at the poles. Examine the value of these two structures as you zoom in and out of the map — they are a representation of the map’s zoom level."