'Unity - trying to add items at specific locations in Mapbox

I´ve followed a tutorial on YouTube where the developer added items spawning around the player at a certain interval with a max distance on the tile in MapBox. What I am trying to achieve is to add them at specific longitude and latitudes.

The changes I made was to to loop through the max amount of items I want to add, incrementing an i variable. Then instead of spawning the item around the player like newLat = getPlayerPosition() + random; I hardcoded the lat and lon like: case 0: newLat = 48.30791f; newLon = 18.08611f; millenium_item.tag = "MI0"; break;

And each item is added to a list of items. The behaviour I am expecting is to add each item to the list and then spawn them on the map, but instead only one item gets added, the one that has the exact same latitude and longitude as the player:

case 3: newLat = 48.32232f; newLon = 18.09462f; millenium_item.tag = "MI3"; break;

Case 3 gets added as it has the same Lat and Lon as my player (where the phone actually is).

case 4: newLat = 48.32232f; newLon = 18.09538f; millenium_item.tag = "MI4"; break;

Case 4 doesnt get added, but as you can see it is pretty close to the location of Case 3.

I dont know what I am doing wrong. Full code:

void SpawnItems() {

        itemindex = 100;
        for(int i = 0; i < count; i++)
        {
            if (googleSignIn.locationdata[i] == false)
            {
            MilleniumItemType type = (MilleniumItemType)(int)UnityEngine.Random.Range(0, Enum.GetValues(typeof(MilleniumItemType)).Length);
            float newLat = 0; 
            float newLon = 0;

            MilleniumPuzzle prefab = Resources.Load("MilleniumItems/puzzle", typeof(MilleniumPuzzle)) as MilleniumPuzzle;
            MilleniumPuzzle millenium_item = Instantiate(prefab, Vector3.zero, Quaternion.Euler(-100, 0, 0)) as MilleniumPuzzle;
            millenium_item.tileManager = tileManager;

                switch (i)
                {
                     ...
                case 3: newLat = 48.32232f; newLon = 18.09462f; millenium_item.tag = "MI3"; break;
                case 4: newLat = 48.32232f; newLon = 18.09538f; millenium_item.tag = "MI4"; break;
                case 5: newLat = 48.32310f; newLon = 18.09536f; millenium_item.tag = "MI5"; break;
                     ...
                }
                /// OUR LAT: 48.32232 OUR LON: 18.09462

                    millenium_item.Init(newLat, newLon);
                    items.Add(millenium_item);
        }
        }

Then Init looks like this:

public void Init(float _lat, float _lon) {
    lat = _lat;
    lon = _lon;
    UpdatePosition ();
}

and:

public void UpdatePosition() {
    float x, y;
    Vector3 position = Vector3.zero;

    geodeticOffsetInv (tileManager.getLat * Mathf.Deg2Rad, tileManager.getLon * Mathf.Deg2Rad, lat * Mathf.Deg2Rad, lon * Mathf.Deg2Rad, out x, out y);

    if ((lat - tileManager.getLat) < 0 && (lon - tileManager.getLon) > 0 || (lat - tileManager.getLat) > 0 && (lon - tileManager.getLon) < 0) {
        position = new Vector3(x, .5f, y);
    }
    else {
        position = new Vector3 (-x, .5f, -y);
    } 

    position.x *= 0.300122f;
    position.z *= 0.123043f;

    Debug.Log("Position of inited millenium puzzle: " + position.x + " z: " + position.z);

    transform.position = position;
}


Solution 1:[1]

Its hard to say exactly what is causing this, but this block of code has a good chance of getting an index out of range exception.

for(int i = 0; i < count; i++)
{
    if (googleSignIn.locationdata[i] == false)
    {

Instead, you should do:

for(int i = 0; i < googleSignIn.locationdata.length; i++)
{
    if (googleSignIn.locationdata[i] == false)
    {

But, there is another problem.

Both this class and the googleSignIn class initialize themselves in Start(), and you are not guaranteed that googleSignIn will execute first. To fix this, you should move the SpawnItems(); call to Awake() instead of Start()

Solution 2:[2]

If you are using MapBox, the Location-based game example has a map where you can add Layers now. Choose the Data source 'Mapbox Streets' and then Add a Point of interest using the specify long/latitude option. This lets you spawn prefabs at those coordinates.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leo Bartkus
Solution 2 Bryan