Map Directions
This example shows how to configure a NGLMapView object. The NGLMapView object is a map view that can be used to display maps from the nominatim database. The NGLMapView allow to sets the following properties :
-
showsScale: This property is set to false to disable the scale bar.
-
scaleBarPosition: This property is set to .topLeft to position the scale bar at the top left of the map view.
-
scaleBarMargins: This property can be set to CGPoint to set the margins of the scale bar.
-
compassView.isHidden: This property can be set to true or false to show or hide the compass view.
-
compassViewMargins: This property can be set to CGPoint to set the margins of the compass view.
-
logoView.isHidden: This property can be set to true or false to show or hide the logo view.
-
logoViewMargins: This property can be set to CGPoint to set the margins of the logo view.
-
showsUserLocation: This property can be set to true or false to show or hide the user's location on the map.
-
userTrackingMode: This property can be set to .follow , .followWithHeading .followWithCourse or .none to make the map follow the user's location.
-
showsUserHeadingIndicator: This property can be set to true or false to enable or disable the user's heading indicator.
-
displayHeadingCalibration: This property can be set to true or false to show or hide the heading calibration alert when necessary.
-
targetCoordinate: This property can be set to the coordinate of the map's target.
-
isScrollEnabled: This property can be set to true or false to allow or disallow the user to scroll the map.
-
panScrollingMode: This property is set to .default to allow the user to scroll the map both horizontally and vertically.
-
isRotateEnabled: This property can be set to true or false to enable or disable the user's ability to rotate the map.
-
isPitchEnabled: This property can be set to true or false to allow or disallow the user to change the pitch of the map.
-
anchorRotateOrZoomGesturesToCenterCoordinate: This property can be set to true or false to enable or disable the anchoring of gestures to the center coordinate of the map while rotating or zooming.
Users can also use the following properties to get information from the NGLMapView object.
-
selectedAnnotations : Gets the selected annotations on the map.
-
visibleAnnotations: Gets the visible annotations that display on the map.
-
annotations : Gets all annotations added on the map.
-
visibleAnnotations: Gets the list of annotations associated with the receiver that intersect with the given rectangle.
-
zoomLevel: Gets the current zoom level of the map view.
-
Style: Gets the current map style.
-
userLocation: Gets the annotation object indicating the user's current location.
-
isRotateEnabled: Gets the bool value that determines whether the user may rotate the map, changing the direction.
-
isPitchEnabled: Gets the bool value that determines whether the user may change the pitch (tilt) of the map.
-
Direction: Gets the direction of the map.
For all code examples, refer to Maps Code Examples
MapConfigViewController view source
1import UIKit2import Nbmap34class MapConfigViewController: UIViewController, NGLMapViewDelegate {5var nbMapView: NGLMapView! {6didSet {7oldValue?.removeFromSuperview()8if let mapView = nbMapView {9configureMapView(nbMapView)10view.insertSubview(mapView, at: 0)11}12}13}1415func configureMapView(_ mapView: NGLMapView) {16mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]17/**18Set the map view's delegate.19A map view sends messages to its delegate to notify it of changes to its20contents or the viewpoint. The delegate also provides information about21annotations displayed on the map, such as the styles to apply to individual22annotations.23If set the delegate the current view controller need to extend with `NGLMapViewDelegate`24*/25mapView.delegate = self2627/**28A Boolean value indicating whether the map may display scale information.29The scale bar may not be shown at all zoom levels. The scale bar becomes visible30when the maximum distance visible on the map view is less than 400 miles (80031kilometers). The zoom level where this occurs depends on the latitude at the map32view's center coordinate, as well as the device screen width. At latitudes33farther from the equator, the scale bar becomes visible at lower zoom levels.34The unit of measurement is determined by the user's device locale.35The view controlled by this property is available at `scaleBar`. The default value36of this property is `NO`.37*/38mapView.showsScale = false3940/**41The position of the scale bar. The default value is `.topLeft`. The optional parameters are: `.topLeft`,`.topRight`,`.bottomLeft`,`.bottomRight`42*/43mapView.scaleBarPosition = .topLeft44/**45A `CGPoint` indicating the position offset of the scale bar.46*/47mapView.scaleBarMargins = CGPoint(x: 10, y: 10)4849// Show or hidden the compass view50mapView.compassView.isHidden = false5152// Set the compass view margins53mapView.compassViewMargins = CGPoint(x: 16 ,y: 100)5455/**56The position of the compass view. The default value is `.topLeft`. The optional parameters are: `.topLeft`,`.topRight`,`.bottomLeft`,`.bottomRight`57*/58mapView.compassViewPosition = .topRight5960/** A `CGPoint` indicating the position offset of the compass view .**/61mapView.compassViewMargins = CGPoint(x: 16 ,y: 100)6263// Show or hidden the logo view64mapView.logoView.isHidden = false65/**66The position of the logo view. The default value is `.bopLeft`. The optional parameters are: `.topLeft`,`.topRight`,`.bottomLeft`,`.bottomRight`67*/68mapView.logoViewPosition = .bottomLeft6970/** A `CGPoint` indicating the position offset of the logo. */71mapView.logoViewMargins = CGPoint(x: 16 ,y: 100)7273/**74A Boolean value indicating whether the map may display the user location.75Setting this property to `YES` causes the map view to use the Core Location76framework to find the current location. As long as this property is `YES`, the77map view continues to track the user's location and update it periodically.78This property does not indicate whether the user's position is actually visible79on the map, only whether the map view is allowed to display it. To determine80whether the user's position is visible, use the `userLocationVisible` property.81The default value of this property is `NO`.82Your app must specify a value for `NSLocationWhenInUseUsageDescription` or83`NSLocationAlwaysUsageDescription` in its `Info.plist` to satisfy the84requirements of the underlying Core Location framework when enabling this85property.86If you implement a custom location manager, set the `locationManager` before87calling `showsUserLocation`.88*/89mapView.showsUserLocation = true9091/**92The mode used to track the user location. The default value is93`NGLUserTrackingModeNone`.94The optional parameters are: `.none`, `.follow`.`.followWithHeading`,`.followWithCourse`.95Changing the value of this property updates the map view with an animated96transition. If you don't want to animate the change, use the97`-setUserTrackingMode:animated:` method instead.98*/99mapView.userTrackingMode = .follow100101/**102A Boolean value indicating whether the user location annotation may display a103permanent heading indicator.104Setting this property to `true` causes the default user location annotation to105appear and always show an arrow-shaped heading indicator, if heading is106available. This property does not rotate the map; for that, see107`NGLUserTrackingModeFollowWithHeading`.108This property has no effect when `userTrackingMode` is109`NGLUserTrackingModeFollowWithHeading` or110`NGLUserTrackingModeFollowWithCourse`.111The default value of this property is `false`.112*/113mapView.showsUserHeadingIndicator = false114115/**116Whether the map view should display a heading calibration alert when necessary.117The default value is `true`.118*/119mapView.displayHeadingCalibration = true120121/**122The geographic coordinate that is the subject of observation as the user123location is being tracked.124By default, this property is set to an invalid coordinate, indicating that125there is no target. In course tracking mode, the target forms one of two foci126in the viewport, the other being the user location annotation. Typically, this127property is set to a destination or waypoint in a real-time navigation scene.128As the user annotation moves toward the target, the map automatically zooms in129to fit both foci optimally within the viewport.130This property has no effect if the `userTrackingMode` property is set to a131value other than `NGLUserTrackingModeFollowWithCourse`.132Changing the value of this property updates the map view with an animated133transition. If you don't want to animate the change, use the134`-setTargetCoordinate:animated:` method instead.135*/136mapView.targetCoordinate = CLLocationCoordinate2DMake(12.94798778, 77.57375084)137138/**139A Boolean value that determines whether the user may zoom the map in and140out, changing the zoom level.141When this property is set to `true`, the default, the user may zoom the map142in and out by pinching two fingers or by double tapping, holding, and moving143the finger up and down.144This property controls only user interactions with the map. If you set the145value of this property to `false`, you may still change the map zoom146programmatically.147*/148mapView.isZoomEnabled = true149150/**151A Boolean value that determines whether the user may scroll around the map,152changing the center coordinate.153When this property is set to `true`, the default, the user may scroll the map154by dragging or swiping with one finger.155This property controls only user interactions with the map. If you set the156value of this property to `false`, you may still change the map location157programmatically.158*/159mapView.isScrollEnabled = true160161/**162The scrolling mode the user is allowed to use to interact with the map.163`NGLPanScrollingModeHorizontal` only allows the user to scroll horizontally on the map,164restricting a user's ability to scroll vertically.165`NGLPanScrollingModeVertical` only allows the user to scroll vertically on the map,166restricting a user's ability to scroll horizontally.167`NGLPanScrollingModeDefault` allows the user to scroll both horizontally and vertically168on the map.169By default, this property is set to `NGLPanScrollingModeDefault`.170*/171mapView.panScrollingMode = .default172173/**174A Boolean value that determines whether the user may rotate the map,175changing the direction.176When this property is set to `true`, the default, the user may rotate the map177by moving two fingers in a circular motion.178This property controls only user interactions with the map. If you set the179value of this property to `false`, you may still rotate the map180programmatically.181*/182mapView.isRotateEnabled = false183184/**185A Boolean value that determines whether the user may change the pitch (tilt) of186the map.187When this property is set to `true`, the default, the user may tilt the map by188vertically dragging two fingers.189This property controls only user interactions with the map. If you set the190value of this property to `false`, you may still change the pitch of the map191programmatically.192The default value of this property is `true`.193*/194mapView.isPitchEnabled = true195/**196A Boolean value that determines whether gestures are anchored to the center coordinate of the map while rotating or zooming.197Default value is set to `false`..198*/199mapView.anchorRotateOrZoomGesturesToCenterCoordinate = false200/**201A floating-point value that determines the rate of deceleration after the user202lifts their finger.203Your application can use the `NGLMapViewDecelerationRate.normal` and204`NGLMapViewDecelerationRate.fast` constants as reference points for reasonable205deceleration rates. `NGLMapViewDecelerationRate.immediate` can be used to206disable deceleration entirely.207*/208mapView.decelerationRate = NGLMapViewDecelerationRate.normal.rawValue209210/** The zoom level of the receiver.211In addition to affecting the visual size and detail of features on the map,212the zoom level affects the size of the vector tiles that are loaded. At zoom213level 0, each tile covers the entire world map; at zoom level 1, it covers 1/4214of the world; at zoom level 2, <sup>1</sup>⁄<sub>16</sub> of the world, and215so on.216Changing the value of this property updates the map view immediately. If you217want to animate the change, use the `-setZoomLevel:animated:` method instead.218*/219mapView.zoomLevel = 18220/**221* The minimum zoom level at which the map can be shown.222*223* Depending on the map view's aspect ratio, the map view may be prevented224* from reaching the minimum zoom level, in order to keep the map from225* repeating within the current viewport.226*227* If the value of this property is greater than that of the228* maximumZoomLevel property, the behavior is undefined.229*230* The default minimumZoomLevel is 0.231*/232mapView.minimumZoomLevel = 0233/**234* The maximum zoom level the map can be shown at.235*236* If the value of this property is smaller than that of the237* minimumZoomLevel property, the behavior is undefined.238*239* The default maximumZoomLevel is 22. The upper bound for this property240* is 25.5.241*/242mapView.maximumZoomLevel = 22243244/**245The heading of the map, measured in degrees clockwise from true north.246The value `0` means that the top edge of the map view corresponds to true247north. The value `90` means the top of the map is pointing due east. The248value `180` means the top of the map points due south, and so on.249Changing the value of this property updates the map view immediately. If you250want to animate the change, use the `-setDirection:animated:` method instead.251*/252mapView.direction = 0253/**254The minimum pitch of the map's camera toward the horizon measured in degrees.255If the value of this property is greater than that of the `maximumPitch`256property, the behavior is undefined. The pitch may not be less than 0257regardless of this property.258The default value of this property is 0 degrees, allowing the map to appear259two-dimensional.260*/261mapView.minimumPitch = 0262/**263The maximum pitch of the map's camera toward the horizon measured in degrees.264If the value of this property is smaller than that of the `minimumPitch`265property, the behavior is undefined. The pitch may not exceed 60 degrees266regardless of this property.267The default value of this property is 60 degrees.268*/269mapView.maximumPitch = 60270271/**272The coordinate bounds visible in the receiver's viewport.273Changing the value of this property updates the receiver immediately. If you274want to animate the change, call `-setVisibleCoordinateBounds:animated:`275instead.276If a longitude is less than −180 degrees or greater than 180 degrees, the277visible bounds straddles the antimeridian or international date line. For278example, if both Tokyo and San Francisco are visible, the visible bounds might279extend from (35.68476, −220.24257) to (37.78428, −122.41310).280*/281// mapView.visibleCoordinateBounds282283/**284The distance from the edges of the map view's frame to the edges of the map285view's logical viewport.286When the value of this property is equal to `UIEdgeInsetsZero`, viewport287properties such as `centerCoordinate` assume a viewport that matches the map288view's frame. Otherwise, those properties are inset, excluding part of the289frame from the viewport. For instance, if the only the top edge is inset, the290map center is effectively shifted downward.291292When the map view's superview is an instance of `UIViewController` whose293`automaticallyAdjustsScrollViewInsets` property is `YES`, the value of this294property may be overridden at any time.295296The usage of `automaticallyAdjustsScrollViewInsets` has been deprecated297use the map view's property `NGLMapView.automaticallyAdjustsContentInset`instead.298Changing the value of this property updates the map view immediately. If you299want to animate the change, use the `-setContentInset:animated:completionHandler:`300method instead.301*/302mapView.contentInset = UIEdgeInsets.zero303304/**305URL of the style currently displayed in the receiver.306The URL may be a full HTTP or HTTPS URL, a Nbmap307style URL (`nbmap://styles/{user}/{style}`), or a path to a local file308relative to the application's resource path.309If you set this property to `nil`, the receiver will use the default style310and this property will automatically be set to that style's URL.311If you want to modify the current style without replacing it outright, or if312you want to introspect individual style attributes, use the `style` property.313*/314// self.mapView?.styleURL = URL(string: styleUrl)315/**316The options that determine which debugging aids are shown on the map.317These options are all disabled by default and should remain disabled in318released software for performance and aesthetic reasons.319*/320321}322323override func viewDidLoad() {324super.viewDidLoad()325/**326Initialize the map view327*/328nbMapView = NGLMapView(frame:self.view.bounds)329/**330Get the select annotations on the map331*/332let selectedAnnotations = nbMapView.selectedAnnotations333/**334Get the visible annotations that display on the map335*/336let visibleAnnotations = nbMapView.visibleAnnotations337338/**339Get all annotations added on the map340*/341let annotation = nbMapView.annotations342343/**344Get the list of annotations associated with the receiver that intersect with345the given rectangle.346*/347let visibleAnnotationsInRect = nbMapView.visibleAnnotations(in: CGRect(x: 100, y: 100, width: 100, height: 100))348349/**350Get the current zoom level of the map view351*/352let zoomLevel = nbMapView.zoomLevel353354/**355Get the current map style356*/357let style = nbMapView.style ?? NGLStyle()358359/**360Get the annotation object indicating the user's current location.361*/362let userLocation = nbMapView.userLocation ?? NGLUserLocation()363364/**365Get the bool value that whether the user may rotate the map, changing the direction.366*/367let isRotateEnabled: Bool = nbMapView.isRotateEnabled368/**369Get the bool value that whether the user may change the pitch (tilt) of the map.370**/371372let isPitchEnabled: Bool = nbMapView.isPitchEnabled373374/**375Get the direction of the map376*/377let direction: CLLocationDirection = nbMapView.direction378379}380381}