Assignment #5 Extra Task #2

Please note, this blog entry is from a previous course. You might want to check out the current one.

Allow the user to switch between a normal map view, a satellite map view and a hybrid map view (you’ll likely want to use a UISegmentedControl for this).

During the lazy instantiation of the map view add an additional sub view for a control segment. The control segment is initialized with an array holding the three possible selections. Its position is calculated by using the horizontal center of the map, and subtracting the size of the segment from the height of the map. To enable auto-rotation the the left and right as well as the top margin are set to be flexible (equal to a spring in story board). The “normal” view is set as initial view. When the value of the control segment is changed, the method changeMapType: is called.

NSArray *controllArray = [NSArray arrayWithObjects:
    @"Normal", @"Satellite", @"Hybrid", nil];
UISegmentedControl *segControl = 
    [[UISegmentedControl alloc] initWithItems:controllArray];
CGPoint center =;
center.y = _mapView.bounds.origin.y + _mapView.bounds.size.height 
    - segControl.frame.size.height / 2 - 10; = center;
segControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin 
                            | UIViewAutoresizingFlexibleRightMargin 
                            | UIViewAutoresizingFlexibleTopMargin;
segControl.selectedSegmentIndex = 0;
[segControl addTarget:self action:@selector(changeMapType:) 
[_mapView addSubview:segControl];

When accessed the map type will be changed:

- (void) changeMapType:(id)sender{
    UISegmentedControl *segControl = (UISegmentedControl *)sender;
    switch ([segControl selectedSegmentIndex]) {
        case 0:
            self.mapView.mapType = MKMapTypeStandard;
        case 1:
            self.mapView.mapType = MKMapTypeSatellite;
        case 2:
            self.mapView.mapType = MKMapTypeHybrid;
            self.mapView.mapType = MKMapTypeStandard;

The complete code for this task is available at github.


Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *