Map View (v2.1.x – 2.2.x)
The map is displayed on the screen via NMMapView which is a UIView subclass and is therefore integrated with Cocoa Touch framework. It can be interacted by the user's gestures right after NMSdk is initialized. You can refer to Map Gestures (v2.0.x) section to get more information on all possible user's gestures.
CREATING THE MAP VIEW
To present the map on the screen, create an instance of NMMapView, add it to the view hierarchy and start the SDK with the following code:
#import <NavmiiSDK/NMNavmiiSDK.h> // in implementation - (void)viewDidLoad { [super viewDidLoad]; mapView = [[NMMapView alloc] initWithFrame:frame]; [self.view addSubview:mapView]; [navmiiSDK setActiveMapView:mapView]; [navmiiSDK startWithSettings:[NMConfigurationSettings settingsWithCustomResourcesPath:customResourcesPath offlineMapsPath:mapsPath innerFilesPath:innerFilesPath]]; }
Alternatively, you can create NMMapView in a .xib or a .storyboard file.
GPS COORDINATES - SCREEN POSITION TRANSFORMATION
You can transform CGPoint representing a screen position of a point on the map to NMMapCoordinates object representing corresponding GPS location using NMMapView's projection property, which is an instance of NMMapProjection class, and the following code:
NMMapCoordinates *coordinates = [mapView.projection mapCoordinatesFromScreenPosition:point];
The reverse operation can be performed by NMMapProjection's screenPositionFromMapCoordinates: method.
CGPoint point = [mapView.projection screenPositionFromMapCoordinates:coordinates];
To retrieve current GPS position relative to the screen use NMMapView's gpsPositionAnchorOnScreen readonly property.
CGPoint point = mapView.gpsPositionAnchorOScreen;
Note: south hemisphere latitudes are represented by negative values.
WORKING WITH CAMERA
Typically, you want to adjust the area presented on the map on your app's events. You can do it by using NMMapView's cameraController property (instance of NMCameraController class) which moves camera to a desired position. The camera position itself is represented in Navmii SDK as NMCameraPosition. NMCameraPosition and NMCameraController allow you to perform the following operations:
- Setting map rotation via heading property
- Setting map center via targetLocation property
- Setting map zoom via zoom property
- Setting map tilt via tilt property
This code instatiates an NMCameraPosition:
NMCameraPosition *targetPosition = [[NMCameraPosition initWithTargetLocation:location heading:heading tilt:tilt zoom:zoom;
To move the camera to a position without animation use NMCameraController's moveCameraToPosition: method:
[mapView.cameraController moveCameraToPosition:position];
Alternatively, you can set the desired camera position with animation, using animateCameraToPosition:, animateCameraToPosition:completion:, animateCameraToPosition:duration:completion: methods. In first two cases the camera will be animated with the default duration of 0.5 second.
//sets the map camera's position with duration of 1.5 seconds //and logs out the result when animation finishes or is canceled [mapView.cameraController animateCameraToPosition:targetPosition duration:1.5 completion:^(BOOL finished, BOOL canceled) { if (finished) NSLog(@"animation finished"); if (canceled) NSLog(@"animation canceled"); };
To stop the animation currently in action use NMMapView's stopAnimation method:
[mapView stopAnimation];
AUTOZOOM
The SDK is able to change the map zoom level itself while navigating or free-driving. The SDK supports 2 patterns of zoom level change: autozoom on speed and autozoom at direction.
- Autozoom on speed. The map zoom changes based on the current speed. The map zooms in when the speed is getting lower and zooms out when the speed is getting higher. This pattern is useful for free-driving, i.e. driving without a specific route set up.
- Autozoom at direction. The map zoom changes based on the distance to the next direction. The map zooms in while getting closer to a direction so that the direction would be visible in the top part of the map view while the current location marker is typically located in the bottom part of the map view. The map zooms out once the direction is passed. This pattern is useful while navigating along a route.
To disallow the autozoom set NMCameraController's autozoomEnabled property to NO.
mapView.cameraController.autozoomEnabled = NO;
To switch between the 2 patterns of autozoom, use NMCameraController's autozoomMode property.
// Enables autozoom on speed. mapView.cameraController.autozoomMode = NMAutozoomModeSpeed; // Enables autozoom at directions. mapView.cameraController.autozoomMode = NMAutozoomModeDirection;
HANDLING CAMERA EVENTS
These are the events on which the SDK informs you, if you have a class conforming to NMCameraMovementListener protocol and have added an instance of this class to NMCameraController object's listeners:
- camera movement started
- camera position changed
- camera movement stopped
- camera movement was cancelled
//adds ViewController to cameraController's listeners when Navmii Sdk starts @interface ViewController () <NMCameraMovementListener> @end //in implementation (make sure your ViewController object responds to NMSdkStateChangeListener events) - (void)onSdkStarted { [mapView.cameraController addCameraMovementListener:self]; }
These events have corresponding methods in NMCameraMovementListener protocol:
- (void)onCameraMovementStarted { //handle "movement started" event here } - (void)onCameraMovementIterated { //handle "camera position changed" event here } - (void)onCameraMovementStopped { //handle "camera movement stopped" event here } - (void)onCameraMovementCanceled { //handle "camera movement was canceled" event here }
You also can add multiple listeners to an NMCameraController instance. To detach a listener use removeCameraMovementListener: method.