How to place an object using a predefined lat and long?
I dont understand well how works mapbox and your project but seems it just spawns some objects set in the map visualizer but i want to modify that data to add certain lat/loc that i want and spawn an item there and also know the height of the terrain in that lat/long in unity coordinates
@diegoarx You can look at SpawnOnMap script on ZoomableMap Example to spawn prefabs at specific locations.
i want to modify that data to add certain lat/loc
If you want to have custom data, you might want to create a custom dataset using Mapbox Studio and create a tileset to consume in Unity. Let me know which option solves your problem best and we can make sure you have all the information you need.
I tried the AutomaticWorldSynchronization scene with the SpawnOnMap and in the editor works fine but when i use it in my samsung s7 it fails and the objects appears just on my position instead on the lat/long i specified
I got to solve it but now i see the red virtual buildings and the item right but the ar camera is not relative to them, i face the s7 in the direction where the building should be and it is not there. The red virtual buildings in the AutomaticWorldSynchronization appear well and the spawn item i add is relative to them in the right place i think, the problem would be synchronize the camera direction with the virtual buildings and spawned item. If i add some dataset it wouldnt solve anything either as the problem as it need to be rotated in the right direction to fit with the virtual content
This is solved in SDK 1.4! 👍
Hi, Sorry I open a closed issue. Please feel free to move my question at appropriate thread. How do I do this with AR Mapbox scene?
Thank
@scarffy Use the GeoToWorldPosition method on the AbstractMap to get positions in Map space. You can use these positions to spawn your objects.
Or if you want to have custom data, you might want to create a custom dataset using Mapbox Studio and create a tileset to consume in Unity. Let me know which option solves your problem best and we can make sure you have all the information you need.
@atripathi-mb Sorry I'm a bit of newbie here. Currently I'm using WorldScaleAR example. Do you mean use GeoToWorldPosition like this?
var locationString = _locationStrings;
var instance = Instantiate(_markerPrefab);
_locationStrings = Conversions.StringToLatLon(locationString);
instance.transform.localPosition = _map.GeoToWorldPosition(_locations, true);
instance.transform.localScale = new Vector3(_spawnScale, _spawnScale, _spawnScale);
What I see currently, the object is right in front of my AR camera. After I walk a few step, it suddenly disappear and that is not what I wanted.
For custom data, the tutorial to integrate with Unity is mostly on SDK 1.2. I couldn't find any tutorial for SDK 1.4 and it is quite confusing for me. Since the method to do it is different.
Edit : I can confirm that it is working properly using zoomable map example but not with AR example
@scarffy Using the SpawnOnMap script should do what you are aiming at. I did not test it on device yet but I am pretty sure it should work. One thing to note is the positions need to be updated as you move since the map gets transformed to align with the real world ( SpawnOnMap script does this in the Update loop)
@scarffy We are in the process of updating our tutorials, will publish them pretty soon.
@atripathi-mb I can confirm even if I'm not walking, the model just disappear after few seconds.
edit : I can provide you a video if you want
@scarffy Yes, a video would be good. Also, if you can share the sample script of how to reproduce this issue , that would be awesome.
@atripathi-mb Here is the link to the video. What I'm trying to do, I want to spawn the model exactly on the power box location at the end of the video.
Here is the script used in the video. Using the same script from SpawnOnMap, I added a little delay before it can spawn the model.
`[SerializeField] AbstractMap _map;
[SerializeField]
[Geocode]
string[] _locationStrings;
Vector2d[] _locations;
[SerializeField]
float _spawnScale = 3f;
[SerializeField]
GameObject _markerPrefab;
List<GameObject> _spawnedObjects;
public Transform parentAnchor;
bool isDelay = true;
void Start(){
StartCoroutine(delaySpawn());
}
IEnumerator delaySpawn(){
yield return new WaitForSeconds(10f);
_locations = new Vector2d[_locationStrings.Length];
_spawnedObjects = new List<GameObject>();
for (int i = 0; i < _locationStrings.Length; i++)
{
var locationString = _locationStrings[i];
_locations[i] = Conversions.StringToLatLon(locationString);
var instance = Instantiate(_markerPrefab, parentAnchor);
instance.transform.localPosition = _map.GeoToWorldPosition(_locations[i], true);
instance.transform.localScale = new Vector3(_spawnScale, _spawnScale, _spawnScale);
_spawnedObjects.Add(instance);
}
isDelay = false;
}
void Update(){
if (isDelay)
return;
int count = _spawnedObjects.Count;
for (int i = 0; i < count; i++){
var spawnedObject = _spawnedObjects[i];
var location = _locations[i];
spawnedObject.transform.localPosition = _map.GeoToWorldPosition(location, true);
spawnedObject.transform.localScale = new Vector3(_spawnScale, _spawnScale, _spawnScale);
}
}
}`
Edit : For the process of what I did, I add this script in WorldAlignmentKit > ArAlignedMap prefab. Then drag Abstract Map to Map variable from ARAlignmentMap.
Hi @atripathi-mb, Do you have any tutorial link for me to use custom data and spawn gameobject at point of interest?
@scarffy I tried the script you shared with the WorldScaleAr scene and it works as expected. Could you give me some more details to help reproduce the issue and get your issue all sorted.
For custom data, I would suggest creating a tileset with your data and using that in Unity to render game-objects. Here is a tutorial to create a dataset & adding it to a tileset.
@atripathi-mb What kind of details do you want me to provide? I'm kinda lost here.
I couldn't find the link you provided in the comment. Can you please check?
Thank you
@scarffy Sorry I forgot to paste the link https://www.mapbox.com/help/studio-manual-datasets 🤦♂️
I am looking for a sample project or script that I can use to reproduce your issue.
Anything that might help me debug, get more insight into the issue will help.
The SpawnOnMap script and the script you provided is working as expected for me, I am not sure why you are seeing the model disappear.
@atripathi-mb No worries :) . I'll try out this custom dataset solution. The spawning on dataset should be similar to SpawnOnMap right?
Here the link to my sample project files. I'm using WorldScaleAr scene in example folder.
https://www.dropbox.com/sh/3dubf1rk9djxax8/AABhZ93sCOF_kSy5BQkypClja?dl=0
@scarffy If you create a custom dataset, you can directly create a vector layer visualizer
You can specify the mapId of your tileset under Custom option of the style name dropdown of Vector.

You can use the PrefabModifier to spawn a prefab at the feature location.
Pro tip : You can add multiple mapIds separated by commas.
@scarffy I looked at a your projects and now I have a clear understanding of what the issue is. Thanks for sending the project. I would suggest deleting the dropbox folder now.
Few issues here
- Your pipe prefab contains a lot of geometry, spawn on map script places the model at that specific location, are you sure the orientation of the pipes matches the real world? Also, I feel the scale of the model is pretty small.
- You were setting the
Parent Anchoras ARRoot, this is going to make your pipe prefab move & rotate with your ARRoot (basically tied to your phone's camera) , I would suggest changing it to ARAligned Map.
Also, I would highly suggest creating a custom dataset with each component ( valves, power box, pipes etc ) digitized as features and then using those features in Unity as layers to setup your prefabs. That way they will be at the real geographic location and you can see them using world scale AR.
You are creating a super interesting use case. Let us know what we can do to help you out.
@atripathi-mb No no. I should be thanking you for helping me.
For issues :-
-
I have ~~double~~ triple checked with mapbox's coordinate debug and google map. So, it should be correct orientation. ~~I'm not sure about the scale being small but i'll check it again just in case.~~ The scale were right.
-
You're right! It's doesn't disappear anymore and it stays. Now, i'm trying to solve the problem where it's not on the ground and of course the coordinate. It doesn't seems to put it in the right place especially if I don't move when I first open the apps. If I move a little, it somehow almost put it in the right place. (Doesn't it has anything to do with getting the initial coordinate and the next coordinate?)
Okay. I will go with your suggestion on custom dataset but first, I need to understand how to use those features first :) (I got confused as the current sdk I used and tutorial is not the same sdk) I'll be back.