Skip to content

Commit a68132d

Browse files
abhishektripapavani
authored andcommitted
Disable InitializeOnStart when InitializeWithLocationProvider is used. (#656)
* Disable initialize on start for AR cases. * Add check to make sure map is initialized before updating tiles. * Updated changelog
1 parent 3e8f961 commit a68132d

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

documentation/docs/05-changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
- Fix foldouts not retaining states between play mode & editor.
2222
- Add missing tooltips.
2323
- Fix issue with Satellite TextureType.
24+
- Added a check to prevent NRE on tile update because map was not initialized.
25+
- Added method to disable `InitializeOnStart` in the `Initialize With Location Provider` script.
2426

2527
### v.1.4.0
2628
*03/20/2018*

sdkproject/Assets/Mapbox/Unity/Map/AbstractMap.cs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,28 @@ public void SetUpPlacement(AbstractMap map)
6262
}
6363
/// <summary>
6464
/// Abstract map.
65-
/// This is the main monobehavior which controls the map. It controls the visualization of map data.
66-
/// Abstract map encapsulates the image, terrain and vector sources and provides a centralized interface to control the visualization of the map.
65+
/// This is the main monobehavior which controls the map. It controls the visualization of map data.
66+
/// Abstract map encapsulates the image, terrain and vector sources and provides a centralized interface to control the visualization of the map.
6767
/// </summary>
6868
public class AbstractMap : MonoBehaviour, IMap
6969
{
70+
/// <summary>
71+
/// Setting to trigger map initialization in Unity's Start method.
72+
/// if set to false, Initialize method should be called explicitly to initialize the map.
73+
/// </summary>
7074
[SerializeField]
7175
private bool _initializeOnStart = true;
76+
public bool InitializeOnStart
77+
{
78+
get
79+
{
80+
return _initializeOnStart;
81+
}
82+
set
83+
{
84+
_initializeOnStart = value;
85+
}
86+
}
7287
/// <summary>
7388
/// The map options.
7489
/// Options to control the behaviour of the map like location,extent, scale and placement.
@@ -87,7 +102,7 @@ public MapOptions Options
87102
}
88103
}
89104
/// <summary>
90-
/// Options to control the imagery component of the map.
105+
/// Options to control the imagery component of the map.
91106
/// </summary>
92107
[SerializeField]
93108
ImageryLayer _imagery = new ImageryLayer();
@@ -114,8 +129,8 @@ public TerrainLayer Terrain
114129
}
115130
/// <summary>
116131
/// The vector data.
117-
/// Options to control the vector data component of the map.
118-
/// Adds a vector source and visualizers to define the rendering behaviour of vector data layers.
132+
/// Options to control the vector data component of the map.
133+
/// Adds a vector source and visualizers to define the rendering behaviour of vector data layers.
119134
/// </summary>
120135
[SerializeField]
121136
VectorLayer _vectorData = new VectorLayer();
@@ -188,8 +203,8 @@ public int AbsoluteZoom
188203

189204
protected int _initialZoom;
190205
/// <summary>
191-
/// Gets the initial zoom at which the map was initialized.
192-
/// This parameter is useful in calculating the scale of the tiles and the map.
206+
/// Gets the initial zoom at which the map was initialized.
207+
/// This parameter is useful in calculating the scale of the tiles and the map.
193208
/// </summary>
194209
/// <value>The initial zoom.</value>
195210
public int InitialZoom
@@ -233,8 +248,8 @@ public float WorldRelativeScale
233248
}
234249
}
235250
/// <summary>
236-
/// Gets the current zoom value of the map.
237-
/// Use <c>AbsoluteZoom</c> to get the zoom level of the tileset.
251+
/// Gets the current zoom value of the map.
252+
/// Use <c>AbsoluteZoom</c> to get the zoom level of the tileset.
238253
/// <seealso cref="AbsoluteZoom"/>
239254
/// </summary>
240255
/// <value>The zoom.</value>
@@ -300,8 +315,8 @@ protected IEnumerator SetupAccess()
300315
}
301316
/// <summary>
302317
/// Sets up map.
303-
/// This method uses the mapOptions and layer properties to setup the map to be rendered.
304-
/// Override <c>SetUpMap</c> to write custom behavior to map setup.
318+
/// This method uses the mapOptions and layer properties to setup the map to be rendered.
319+
/// Override <c>SetUpMap</c> to write custom behavior to map setup.
305320
/// </summary>
306321
protected virtual void SetUpMap()
307322
{
@@ -332,7 +347,7 @@ protected virtual void SetUpMap()
332347
if (_options.extentOptions.extentType != MapExtentType.Custom)
333348
{
334349
ITileProviderOptions tileProviderOptions = _options.extentOptions.GetTileProviderOptions();
335-
// Setup tileprovider based on type.
350+
// Setup tileprovider based on type.
336351
switch (_options.extentOptions.extentType)
337352
{
338353
case MapExtentType.CameraBounds:
@@ -431,7 +446,7 @@ protected virtual void InitializeMap(MapOptions options)
431446
}
432447
/// <summary>
433448
/// Initialize the map using the specified latLon and zoom.
434-
/// Map will automatically get initialized in the <c>Start</c> method.
449+
/// Map will automatically get initialized in the <c>Start</c> method.
435450
/// Use this method to explicitly initialize the map and disable intialize on <c>Start</c>
436451
/// </summary>
437452
/// <returns>The initialize.</returns>
@@ -451,9 +466,9 @@ public virtual void Initialize(Vector2d latLon, int zoom)
451466
}
452467
/// <summary>
453468
/// Updates the map.
454-
/// Use this method to update the location of the map.
455-
/// Update method should be used when panning, zooming or changing location of the map.
456-
/// This method avoid startup delays that might occur on re-initializing the map.
469+
/// Use this method to update the location of the map.
470+
/// Update method should be used when panning, zooming or changing location of the map.
471+
/// This method avoid startup delays that might occur on re-initializing the map.
457472
/// </summary>
458473
/// <param name="latLon">LatitudeLongitude.</param>
459474
/// <param name="zoom">Zoom level.</param>
@@ -473,7 +488,7 @@ public virtual void UpdateMap(Vector2d latLon, float zoom)
473488
xDelta = xDelta > 0 ? Mathd.Min(xDelta, Mapbox.Utils.Constants.LatitudeMax) : Mathd.Max(xDelta, -Mapbox.Utils.Constants.LatitudeMax);
474489
zDelta = zDelta > 0 ? Mathd.Min(zDelta, Mapbox.Utils.Constants.LongitudeMax) : Mathd.Max(zDelta, -Mapbox.Utils.Constants.LongitudeMax);
475490

476-
//Set Center in Latitude Longitude and Mercator.
491+
//Set Center in Latitude Longitude and Mercator.
477492
SetCenterLatitudeLongitude(new Vector2d(xDelta, zDelta));
478493
Options.scalingOptions.scalingStrategy.SetUpScaling(this);
479494

@@ -489,7 +504,7 @@ public virtual void UpdateMap(Vector2d latLon, float zoom)
489504
}
490505
/// <summary>
491506
/// Resets the map.
492-
/// Use this method to reset the map to and reset all parameters.
507+
/// Use this method to reset the map to and reset all parameters.
493508
/// </summary>
494509
public void ResetMap()
495510
{
@@ -545,7 +560,7 @@ protected void SendInitialized()
545560

546561
internal Vector3 GeoToWorldPositionXZ(Vector2d latitudeLongitude)
547562
{
548-
// For quadtree implementation of the map, the map scale needs to be compensated for.
563+
// For quadtree implementation of the map, the map scale needs to be compensated for.
549564
var scaleFactor = Mathf.Pow(2, (InitialZoom - AbsoluteZoom));
550565
var worldPos = Conversions.GeoToWorldPosition(latitudeLongitude, CenterMercator, WorldRelativeScale * scaleFactor).ToVector3xz();
551566
return Root.TransformPoint(worldPos);
@@ -570,7 +585,7 @@ protected virtual float QueryElevationAtInternal(Vector2d latlong, out float til
570585

571586
}
572587
/// <summary>
573-
/// Converts a latitude longitude into map space position.
588+
/// Converts a latitude longitude into map space position.
574589
/// </summary>
575590
/// <returns>Position in map space.</returns>
576591
/// <param name="latitudeLongitude">Latitude longitude.</param>
@@ -589,13 +604,13 @@ public virtual Vector3 GeoToWorldPosition(Vector2d latitudeLongitude, bool query
589604
return worldPos;
590605
}
591606
/// <summary>
592-
/// Converts a position in map space into a laitude longitude.
607+
/// Converts a position in map space into a laitude longitude.
593608
/// </summary>
594609
/// <returns>Position in Latitude longitude.</returns>
595610
/// <param name="realworldPoint">Realworld point.</param>
596611
public virtual Vector2d WorldToGeoPosition(Vector3 realworldPoint)
597612
{
598-
// For quadtree implementation of the map, the map scale needs to be compensated for.
613+
// For quadtree implementation of the map, the map scale needs to be compensated for.
599614
var scaleFactor = Mathf.Pow(2, (InitialZoom - AbsoluteZoom));
600615

601616
return (Root.InverseTransformPoint(realworldPoint)).GetGeoPosition(CenterMercator, WorldRelativeScale * scaleFactor);

sdkproject/Assets/Mapbox/Unity/Map/InitializeMapWithLocationProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public class InitializeMapWithLocationProvider : MonoBehaviour
1111

1212
ILocationProvider _locationProvider;
1313

14+
private void Awake()
15+
{
16+
// Prevent double initialization of the map.
17+
_map.InitializeOnStart = false;
18+
}
19+
1420
IEnumerator Start()
1521
{
1622
yield return null;
@@ -24,4 +30,4 @@ void LocationProvider_OnLocationUpdated(Unity.Location.Location location)
2430
_map.Initialize(location.LatitudeLongitude, _map.AbsoluteZoom);
2531
}
2632
}
27-
}
33+
}

sdkproject/Assets/Mapbox/Unity/Map/RangeTileProvider.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@ namespace Mapbox.Unity.Map
77

88
public class RangeTileProvider : AbstractTileProvider
99
{
10-
//[SerializeField]
11-
//private int _west = 2;
12-
//[SerializeField]
13-
//private int _north = 2;
14-
//[SerializeField]
15-
//private int _east = 2;
16-
//[SerializeField]
17-
//private int _south = 2;
1810
RangeTileProviderOptions _rangeTileProviderOptions;
11+
private bool _initialized = false;
1912

2013
public override void OnInitialized()
2114
{
@@ -28,15 +21,16 @@ public override void OnInitialized()
2821
_rangeTileProviderOptions = new RangeTileProviderOptions();
2922
}
3023

31-
32-
//foreach (var tile in tilesToRequest)
33-
//{
34-
// AddTile(tile);
35-
//}
24+
_initialized = true;
3625
}
3726

3827
private void Update()
3928
{
29+
if (!_initialized)
30+
{
31+
return;
32+
}
33+
4034
if (Options == null)
4135
{
4236
return;

0 commit comments

Comments
 (0)