Latitude/Longitude points from CSV file

Hi all,
I’m using a 3D map of Belfast in Unity and I’m trying to add data points to the map (at the minute, just little red spheres until I get the positioning right). I’m trying to place the gameobjects on the globe with data pulled in from a CSV file with latitude and longitude co-ordinate and the rest of the data for those GameObjects. The reason for this is that the dataset can be updated externally and when the game is played, all the Sphere gameobjects will be instantiated. Is there a bit of C# code I can use to populate a CesiumGeoreference component that each Sphere GameObject has? I am currently trying the below snippet but it’s not working:

void PlaceMarkersOnMap()
{
    // Get a reference to the CesiumGeoreference component
    var georeference = GetComponent<CesiumGeoreference>();

    foreach (MapPoint point in myMapPointList.mapPoint)
    {
        // Convert the latitude and longitude values from the CSV file into Unity world coordinates
        Vector3 position = georeference.ComputeCesiumToUnityCoordinates(Cartographic.FromDegrees(point.xCoordinate, point.yCoordinate));

        // Instantiate a marker prefab at the specified position
        GameObject marker = Instantiate(markerPrefab, position, Quaternion.identity, map.transform);

        Debug.Log($"Parsed point {point.name} at ({point.xCoordinate}, {point.yCoordinate})");
    }
}

Has anyone tried anything like this? Any help would be greatly appreciated! Thank you!

Your code doesn’t make much sense to me. For example, CesiumGeoreference doesn’t have a method called ComputeCesiumToUnityCoordinates. And there’s no Cartographic type in Cesium for Unity. Not sure where this code came from?

But to put objects at a particular location on the globe, I’d just add a CesiumGlobeAnchor component to them and then set the component’s longitudeLatitudeHeight property.

Hi Kevin,
Thank you for getting back to me! I agree, the code is nonsense, it’s just placeholders to give the idea of what I’m trying to do! I’m trying to put about 300 data points around the city, and the GameObjects are only instantiated when the game is being played and the script that reads in the CSV file with the info for each GameObject is read in (so they only exist when the game is played, otherwise I just have the prefab of the data point to edit), so I would need to use a script to add the globe anchor and I haven’t been able to find any info on how to do that yet! I hope this makes some sense?

Thanks,

Laura

Yep, makes sense. The globe anchor can be added like any other component:

CesiumGlobeAnchor anchor = marker.AddComponent<CesiumGlobeAnchor>();

Or you can include it in the prefab.

Hi Kevin,

Thanks again for your help! I am trying to instantiate the markers using the below code and I am running into errors:
void PlaceMarkersOnMap()
{
foreach (MapPoint mapPoint in myMapPointList.mapPoint)
{
// Instantiate the marker prefab
GameObject marker = Instantiate(markerPrefab);
marker.name = “Marker”;

        // Add a CesiumGlobeAnchor component to the marker and set the position using longitude and latitude from the MapPoint
        CesiumGlobeAnchor anchor = marker.AddComponent<CesiumGlobeAnchor>();
        anchor.longitudeLatitudeHeight = new double3(mapPoint.xCoordinate, mapPoint.yCoordinate, 90.0);

        // Parent the marker to a GameObject in your scene if you want to organize the markers
        marker.transform.SetParent(map.transform);

        // Activate the marker
        marker.SetActive(true);
    }
}

I am getting an error saying Object Reference not set to an instance of an object, I’m sure I’m just doing something silly, but i’ve been looking at it for so long I can’t figure out what to try next! Do you have any ideas? Thank you :slight_smile:

Hi Kevin,
Please disregard the above, I have got my markers on my map alright now! But i am having issues setting the scale. The Unity scale of my prefab is 10,10,10, but when in the cesium anchor component it’s automatically setting the scale of each instantiated marker to -1,-1,-1 and I have no idea why, or how I would go about correcting this. The code I am now using is as follows:

void PlaceMarkersOnMap()
{
    foreach (MapPoint mapPoint in myMapPointList.mapPoint)
    {
        // Instantiate the marker prefab
        GameObject marker = Instantiate(markerPrefab);
        marker.name = "Marker";

        // Add a CesiumGlobeAnchor component to the marker and set the position using longitude and latitude from the MapPoint
        CesiumGlobeAnchor anchor = marker.AddComponent<CesiumGlobeAnchor>();
        anchor.longitudeLatitudeHeight = new double3(mapPoint.xCoordinate, mapPoint.yCoordinate, 150.0);
        marker.transform.localScale = new Vector3(10f, 10f, 10f);


        // Parent the marker to a GameObject in your scene to organize the markers
        marker.transform.SetParent(map.transform);

        // Activate the marker
        marker.SetActive(true);
    }
}

all my markers are coming up as tiny red spheres instead of big red spheres, and I am, once again, stumped! Sorry for all the silly questions, I really appreciate your patience!

Oh my gosh sorry, I just had to change around two of the lines of code please ignore the above once again! And thank you!

Glad to hear you got it working!

1 Like