MapView Configuration

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

1
import UIKit
2
import Nbmap
3
4
class MapConfigViewController: UIViewController, NGLMapViewDelegate {
5
var nbMapView: NGLMapView! {
6
didSet {
7
oldValue?.removeFromSuperview()
8
if let mapView = nbMapView {
9
configureMapView(nbMapView)
10
view.insertSubview(mapView, at: 0)
11
}
12
}
13
}
14
15
func configureMapView(_ mapView: NGLMapView) {
16
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
17
/**
18
Set the map view's delegate.
19
A map view sends messages to its delegate to notify it of changes to its
20
contents or the viewpoint. The delegate also provides information about
21
annotations displayed on the map, such as the styles to apply to individual
22
annotations.
23
If set the delegate the current view controller need to extend with `NGLMapViewDelegate`
24
*/
25
mapView.delegate = self
26
27
/**
28
A Boolean value indicating whether the map may display scale information.
29
The scale bar may not be shown at all zoom levels. The scale bar becomes visible
30
when the maximum distance visible on the map view is less than 400 miles (800
31
kilometers). The zoom level where this occurs depends on the latitude at the map
32
view's center coordinate, as well as the device screen width. At latitudes
33
farther from the equator, the scale bar becomes visible at lower zoom levels.
34
The unit of measurement is determined by the user's device locale.
35
The view controlled by this property is available at `scaleBar`. The default value
36
of this property is `NO`.
37
*/
38
mapView.showsScale = false
39
40
/**
41
The position of the scale bar. The default value is `.topLeft`. The optional parameters are: `.topLeft`,`.topRight`,`.bottomLeft`,`.bottomRight`
42
*/
43
mapView.scaleBarPosition = .topLeft
44
/**
45
A `CGPoint` indicating the position offset of the scale bar.
46
*/
47
mapView.scaleBarMargins = CGPoint(x: 10, y: 10)
48
49
// Show or hidden the compass view
50
mapView.compassView.isHidden = false
51
52
// Set the compass view margins
53
mapView.compassViewMargins = CGPoint(x: 16 ,y: 100)
54
55
/**
56
The position of the compass view. The default value is `.topLeft`. The optional parameters are: `.topLeft`,`.topRight`,`.bottomLeft`,`.bottomRight`
57
*/
58
mapView.compassViewPosition = .topRight
59
60
/** A `CGPoint` indicating the position offset of the compass view .**/
61
mapView.compassViewMargins = CGPoint(x: 16 ,y: 100)
62
63
// Show or hidden the logo view
64
mapView.logoView.isHidden = false
65
/**
66
The position of the logo view. The default value is `.bopLeft`. The optional parameters are: `.topLeft`,`.topRight`,`.bottomLeft`,`.bottomRight`
67
*/
68
mapView.logoViewPosition = .bottomLeft
69
70
/** A `CGPoint` indicating the position offset of the logo. */
71
mapView.logoViewMargins = CGPoint(x: 16 ,y: 100)
72
73
/**
74
A Boolean value indicating whether the map may display the user location.
75
Setting this property to `YES` causes the map view to use the Core Location
76
framework to find the current location. As long as this property is `YES`, the
77
map view continues to track the user's location and update it periodically.
78
This property does not indicate whether the user's position is actually visible
79
on the map, only whether the map view is allowed to display it. To determine
80
whether the user's position is visible, use the `userLocationVisible` property.
81
The default value of this property is `NO`.
82
Your app must specify a value for `NSLocationWhenInUseUsageDescription` or
83
`NSLocationAlwaysUsageDescription` in its `Info.plist` to satisfy the
84
requirements of the underlying Core Location framework when enabling this
85
property.
86
If you implement a custom location manager, set the `locationManager` before
87
calling `showsUserLocation`.
88
*/
89
mapView.showsUserLocation = true
90
91
/**
92
The mode used to track the user location. The default value is
93
`NGLUserTrackingModeNone`.
94
The optional parameters are: `.none`, `.follow`.`.followWithHeading`,`.followWithCourse`.
95
Changing the value of this property updates the map view with an animated
96
transition. If you don't want to animate the change, use the
97
`-setUserTrackingMode:animated:` method instead.
98
*/
99
mapView.userTrackingMode = .follow
100
101
/**
102
A Boolean value indicating whether the user location annotation may display a
103
permanent heading indicator.
104
Setting this property to `true` causes the default user location annotation to
105
appear and always show an arrow-shaped heading indicator, if heading is
106
available. This property does not rotate the map; for that, see
107
`NGLUserTrackingModeFollowWithHeading`.
108
This property has no effect when `userTrackingMode` is
109
`NGLUserTrackingModeFollowWithHeading` or
110
`NGLUserTrackingModeFollowWithCourse`.
111
The default value of this property is `false`.
112
*/
113
mapView.showsUserHeadingIndicator = false
114
115
/**
116
Whether the map view should display a heading calibration alert when necessary.
117
The default value is `true`.
118
*/
119
mapView.displayHeadingCalibration = true
120
121
/**
122
The geographic coordinate that is the subject of observation as the user
123
location is being tracked.
124
By default, this property is set to an invalid coordinate, indicating that
125
there is no target. In course tracking mode, the target forms one of two foci
126
in the viewport, the other being the user location annotation. Typically, this
127
property is set to a destination or waypoint in a real-time navigation scene.
128
As the user annotation moves toward the target, the map automatically zooms in
129
to fit both foci optimally within the viewport.
130
This property has no effect if the `userTrackingMode` property is set to a
131
value other than `NGLUserTrackingModeFollowWithCourse`.
132
Changing the value of this property updates the map view with an animated
133
transition. If you don't want to animate the change, use the
134
`-setTargetCoordinate:animated:` method instead.
135
*/
136
mapView.targetCoordinate = CLLocationCoordinate2DMake(12.94798778, 77.57375084)
137
138
/**
139
A Boolean value that determines whether the user may zoom the map in and
140
out, changing the zoom level.
141
When this property is set to `true`, the default, the user may zoom the map
142
in and out by pinching two fingers or by double tapping, holding, and moving
143
the finger up and down.
144
This property controls only user interactions with the map. If you set the
145
value of this property to `false`, you may still change the map zoom
146
programmatically.
147
*/
148
mapView.isZoomEnabled = true
149
150
/**
151
A Boolean value that determines whether the user may scroll around the map,
152
changing the center coordinate.
153
When this property is set to `true`, the default, the user may scroll the map
154
by dragging or swiping with one finger.
155
This property controls only user interactions with the map. If you set the
156
value of this property to `false`, you may still change the map location
157
programmatically.
158
*/
159
mapView.isScrollEnabled = true
160
161
/**
162
The 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,
164
restricting a user's ability to scroll vertically.
165
`NGLPanScrollingModeVertical` only allows the user to scroll vertically on the map,
166
restricting a user's ability to scroll horizontally.
167
`NGLPanScrollingModeDefault` allows the user to scroll both horizontally and vertically
168
on the map.
169
By default, this property is set to `NGLPanScrollingModeDefault`.
170
*/
171
mapView.panScrollingMode = .default
172
173
/**
174
A Boolean value that determines whether the user may rotate the map,
175
changing the direction.
176
When this property is set to `true`, the default, the user may rotate the map
177
by moving two fingers in a circular motion.
178
This property controls only user interactions with the map. If you set the
179
value of this property to `false`, you may still rotate the map
180
programmatically.
181
*/
182
mapView.isRotateEnabled = false
183
184
/**
185
A Boolean value that determines whether the user may change the pitch (tilt) of
186
the map.
187
When this property is set to `true`, the default, the user may tilt the map by
188
vertically dragging two fingers.
189
This property controls only user interactions with the map. If you set the
190
value of this property to `false`, you may still change the pitch of the map
191
programmatically.
192
The default value of this property is `true`.
193
*/
194
mapView.isPitchEnabled = true
195
/**
196
A Boolean value that determines whether gestures are anchored to the center coordinate of the map while rotating or zooming.
197
Default value is set to `false`..
198
*/
199
mapView.anchorRotateOrZoomGesturesToCenterCoordinate = false
200
/**
201
A floating-point value that determines the rate of deceleration after the user
202
lifts their finger.
203
Your application can use the `NGLMapViewDecelerationRate.normal` and
204
`NGLMapViewDecelerationRate.fast` constants as reference points for reasonable
205
deceleration rates. `NGLMapViewDecelerationRate.immediate` can be used to
206
disable deceleration entirely.
207
*/
208
mapView.decelerationRate = NGLMapViewDecelerationRate.normal.rawValue
209
210
/** The zoom level of the receiver.
211
In addition to affecting the visual size and detail of features on the map,
212
the zoom level affects the size of the vector tiles that are loaded. At zoom
213
level 0, each tile covers the entire world map; at zoom level 1, it covers 1/4
214
of the world; at zoom level 2, <sup>1</sup>⁄<sub>16</sub> of the world, and
215
so on.
216
Changing the value of this property updates the map view immediately. If you
217
want to animate the change, use the `-setZoomLevel:animated:` method instead.
218
*/
219
mapView.zoomLevel = 18
220
/**
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 prevented
224
* from reaching the minimum zoom level, in order to keep the map from
225
* repeating within the current viewport.
226
*
227
* If the value of this property is greater than that of the
228
* maximumZoomLevel property, the behavior is undefined.
229
*
230
* The default minimumZoomLevel is 0.
231
*/
232
mapView.minimumZoomLevel = 0
233
/**
234
* The maximum zoom level the map can be shown at.
235
*
236
* If the value of this property is smaller than that of the
237
* minimumZoomLevel property, the behavior is undefined.
238
*
239
* The default maximumZoomLevel is 22. The upper bound for this property
240
* is 25.5.
241
*/
242
mapView.maximumZoomLevel = 22
243
244
/**
245
The heading of the map, measured in degrees clockwise from true north.
246
The value `0` means that the top edge of the map view corresponds to true
247
north. The value `90` means the top of the map is pointing due east. The
248
value `180` means the top of the map points due south, and so on.
249
Changing the value of this property updates the map view immediately. If you
250
want to animate the change, use the `-setDirection:animated:` method instead.
251
*/
252
mapView.direction = 0
253
/**
254
The minimum pitch of the map's camera toward the horizon measured in degrees.
255
If the value of this property is greater than that of the `maximumPitch`
256
property, the behavior is undefined. The pitch may not be less than 0
257
regardless of this property.
258
The default value of this property is 0 degrees, allowing the map to appear
259
two-dimensional.
260
*/
261
mapView.minimumPitch = 0
262
/**
263
The maximum pitch of the map's camera toward the horizon measured in degrees.
264
If the value of this property is smaller than that of the `minimumPitch`
265
property, the behavior is undefined. The pitch may not exceed 60 degrees
266
regardless of this property.
267
The default value of this property is 60 degrees.
268
*/
269
mapView.maximumPitch = 60
270
271
/**
272
The coordinate bounds visible in the receiver's viewport.
273
Changing the value of this property updates the receiver immediately. If you
274
want to animate the change, call `-setVisibleCoordinateBounds:animated:`
275
instead.
276
If a longitude is less than −180 degrees or greater than 180 degrees, the
277
visible bounds straddles the antimeridian or international date line. For
278
example, if both Tokyo and San Francisco are visible, the visible bounds might
279
extend from (35.68476, −220.24257) to (37.78428, −122.41310).
280
*/
281
// mapView.visibleCoordinateBounds
282
283
/**
284
The distance from the edges of the map view's frame to the edges of the map
285
view's logical viewport.
286
When the value of this property is equal to `UIEdgeInsetsZero`, viewport
287
properties such as `centerCoordinate` assume a viewport that matches the map
288
view's frame. Otherwise, those properties are inset, excluding part of the
289
frame from the viewport. For instance, if the only the top edge is inset, the
290
map center is effectively shifted downward.
291
292
When the map view's superview is an instance of `UIViewController` whose
293
`automaticallyAdjustsScrollViewInsets` property is `YES`, the value of this
294
property may be overridden at any time.
295
296
The usage of `automaticallyAdjustsScrollViewInsets` has been deprecated
297
use the map view's property `NGLMapView.automaticallyAdjustsContentInset`instead.
298
Changing the value of this property updates the map view immediately. If you
299
want to animate the change, use the `-setContentInset:animated:completionHandler:`
300
method instead.
301
*/
302
mapView.contentInset = UIEdgeInsets.zero
303
304
/**
305
URL of the style currently displayed in the receiver.
306
The URL may be a full HTTP or HTTPS URL, a Nbmap
307
style URL (`nbmap://styles/{user}/{style}`), or a path to a local file
308
relative to the application's resource path.
309
If you set this property to `nil`, the receiver will use the default style
310
and this property will automatically be set to that style's URL.
311
If you want to modify the current style without replacing it outright, or if
312
you want to introspect individual style attributes, use the `style` property.
313
*/
314
// self.mapView?.styleURL = URL(string: styleUrl)
315
/**
316
The options that determine which debugging aids are shown on the map.
317
These options are all disabled by default and should remain disabled in
318
released software for performance and aesthetic reasons.
319
*/
320
321
}
322
323
override func viewDidLoad() {
324
super.viewDidLoad()
325
/**
326
Initialize the map view
327
*/
328
nbMapView = NGLMapView(frame:self.view.bounds)
329
/**
330
Get the select annotations on the map
331
*/
332
let selectedAnnotations = nbMapView.selectedAnnotations
333
/**
334
Get the visible annotations that display on the map
335
*/
336
let visibleAnnotations = nbMapView.visibleAnnotations
337
338
/**
339
Get all annotations added on the map
340
*/
341
let annotation = nbMapView.annotations
342
343
/**
344
Get the list of annotations associated with the receiver that intersect with
345
the given rectangle.
346
*/
347
let visibleAnnotationsInRect = nbMapView.visibleAnnotations(in: CGRect(x: 100, y: 100, width: 100, height: 100))
348
349
/**
350
Get the current zoom level of the map view
351
*/
352
let zoomLevel = nbMapView.zoomLevel
353
354
/**
355
Get the current map style
356
*/
357
let style = nbMapView.style ?? NGLStyle()
358
359
/**
360
Get the annotation object indicating the user's current location.
361
*/
362
let userLocation = nbMapView.userLocation ?? NGLUserLocation()
363
364
/**
365
Get the bool value that whether the user may rotate the map, changing the direction.
366
*/
367
let isRotateEnabled: Bool = nbMapView.isRotateEnabled
368
/**
369
Get the bool value that whether the user may change the pitch (tilt) of the map.
370
**/
371
372
let isPitchEnabled: Bool = nbMapView.isPitchEnabled
373
374
/**
375
Get the direction of the map
376
*/
377
let direction: CLLocationDirection = nbMapView.direction
378
379
}
380
381
}

© 2024 NextBillion.ai all rights reserved.