Question

I have an iOs map-application where the 3D view is utilized by default. Thus, the MKMapCamera is set in the beginning (the pitch value being 45). However, I want to give users an option to disable the 3D-map in case they prefer the 2D-map. So in order to make the switch between views, I'll change the camera's pitch (0 in 2D, 45 in 3D) and disable/enable changing it (setPitchEnabled = NO/YES).

For some reason I'm not able to anymore enable the 3D view after I've once disabled the pitch, i.e. after I've once set [_mapView setPitchEnabled:NO], I cannot change it to YES anymore (After trying to change it back to YES, the pitchEnabled keeps its value as NO when its printed in NSLog). Therefore I cannot get back from 2D to 3D. Do you have any idea what would be causing this? It is very confusing, because I tried enabling and disabling the rotation ([_mapView setRotationEnabled:NO] or YES) in the same manner, and in that case it works perfectly between these two cases. But not with the pitch - after disabling the pitch I'm not able to enable it anymore. Below are the functions that set camera in the beginning (setCamera) and function that toggles between modes (toggleViewMode).

//Function that sets the 3D-view in the beginning

-(void)setCamera {

    CLLocationCoordinate2D userLocation;
    userLocation.latitude = self.locationManager.currentLatitude;
    userLocation.longitude = self.locationManager.currentLongitude;

    if(_cameraSet == YES) {
        NSLog(@"Camera is set already, return");
        return;
    }


    if ([_mapView respondsToSelector:@selector(camera)] && _show3Dmap) {

        _cameraSet = YES;
        [_mapView setShowsBuildings:NO];

        MKMapCamera *newCamera = [[_mapView camera] copy];
        [newCamera setPitch:45.0];
        [newCamera setHeading:0.0];
        [newCamera setAltitude:500.0];

        [_mapView setCamera:newCamera animated:NO];
    } 
}


//Toggle between 2D and 3D
-(void)toggleViewMode:(BOOL)birdView {

    if(birdView == YES) {
        _mapView.camera.pitch = 45;
        [_mapView setPitchEnabled:YES];
    } else {
        _mapView.camera.pitch = 0;
        [_mapView setPitchEnabled:NO];
    }

}

}

Was it helpful?

Solution

Found the answer elsewhere. You have to set a camera also.

Here is a sample camera init :

MKMapCamera *newCamera = [[self.mapView camera] copy];
[newCamera setPitch:45.0];
[newCamera setHeading:90.0];
[newCamera setAltitude:500.0];
[self.mapView setCamera:newCamera animated:YES];

OTHER TIPS

Please see Discussion here at apple document.

[https://developer.apple.com/documentation/mapkit/mkmapview/1452277-camera#discussion][1]

You have to play with the pitch property to toggle between the 2D and 3D map view

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top