Search Unity

Problem with mapbox geo to world position

Discussion in 'Editor & General Support' started by MidoWolf, Nov 15, 2017.

  1. MidoWolf

    MidoWolf

    Joined:
    Feb 25, 2017
    Posts:
    1
    Hi all,
    I'm trying to make a unity app that uses a map and locating system, and i found out about mapbox, i already finished the tutorials provided in their website, but i still can't figure out how i can place objects in the map and points of interest which are probably the basics i guess. I'm using the slippy terrrain example, and i want to place an object somewhere in the city, but the objects are placed in the middle of the map whatever the values of latitude and longitude are,
    Code (CSharp):
    1. transform.position = Conversions.GeoToWorldPosition(new Mapbox.Utils.Vector2d(32.2993900, -9.2371800),
    2.                                                                  _map.CenterMercator,
    3.                                                                  _map.WorldRelativeScale).ToVector3xz();
     
    Last edited: Nov 15, 2017
  2. hexbyte

    hexbyte

    Joined:
    Aug 24, 2014
    Posts:
    4
    Anyone help you?
     
  3. hexbyte

    hexbyte

    Joined:
    Aug 24, 2014
    Posts:
    4
    Bumping this again, I'm having the same issue I actually have a verified lat and long Vector2d I can see the lat and lon in debug window but it does the same as above, and the value is always, 0,0
     
  4. Rhybo

    Rhybo

    Joined:
    Apr 3, 2018
    Posts:
    1
    I am also struggling with this issue. I want to spawn entities (who have their own lat/long location) when the connect with Unity in the appropriate spot.

    Before I'm able to implement that functionality, I'm attempting to get an accurate lat/long reading from the generated terrain by pulling the value directly from the terrain collider with a raycast. However, I think my units are off or zoom level as it seems like my values obtained from Conversions.GeoFromGlobePosition() change to dramatically over a short distance. If anyone has any insight I'm sure we would all like to know because the Mapbox API reference is not great at explaining how their methods work. For reference here is my code, it attempts to pull a lat/long from directly below a projected teleport location (using SteamVR for teleporting around a map):

    Code (CSharp):
    1. //using System.Collections;
    2. //using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mapbox.Unity.Utilities;
    5. //using Mapbox.Unity.Map;
    6. using Mapbox.Utils;
    7. //using Mapbox.Map;
    8.  
    9.  
    10. public class PlayerTeleportLocation : MonoBehaviour {
    11.  
    12.     //public static Vector2d GeoFromGlobePosition(Vector3 point, float radius);
    13.     public Vector2d maplocation = new Vector2d(0.0,0.0);
    14.     public GameObject playerDestination;
    15.     public LayerMask layerMask;  // use the layer that the MapBox map is designated as
    16.     public LineRenderer futurePerch;
    17.  
    18.     public GameObject ScanEagle;
    19.  
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         Vector3[] lineInit = new Vector3[2] { Vector3.zero, Vector3.zero };
    24.         futurePerch.SetPositions(lineInit);  // initialize the perch line indicator to zero
    25.         futurePerch.startWidth = 0.1f;
    26.         futurePerch.endWidth = 0.1f;
    27.         //playerDestination = GameObject.Find("/Teleporting/DestinationReticle");
    28.         //LayerMask map = LayerMask.GetMask("Map");
    29.         //GameObject ScanEagle = GameObject.FindWithTag("NCSasset");
    30.     }
    31.  
    32.    
    33.     void Update ()
    34.     {
    35.         // Is the Teleport Reticle active?
    36.         if (playerDestination.activeSelf)
    37.         {
    38.             RaycastHit hit; //where the data from a raycast hit will be stored
    39.  
    40.             // Is the ray intersecting with the "map" layer?
    41.             if (Physics.Raycast(playerDestination.transform.position, playerDestination.transform.TransformVector(Vector3.down), out hit, Mathf.Infinity))
    42.             {
    43.  
    44.                 Debug.DrawLine(playerDestination.transform.position, hit.point, Color.green, Time.deltaTime, false);
    45.                 Debug.Log(hit.point);
    46.                 Vector3[] lineEndings = new Vector3[2] { playerDestination.transform.position, hit.point}; // the vertical distance from the teleporter marker to the map
    47.                 futurePerch.SetPosition(0, lineEndings[0]);
    48.                 futurePerch.SetPosition(1, lineEndings[1]);
    49.                 futurePerch.enabled = true;
    50.                 maplocation = Conversions.GeoFromGlobePosition(hit.point, 1000f);
    51.                 Debug.Log(maplocation);
    52.             }
    53.             // If the ray isn't hitting anything then deactivate the line
    54.             else { futurePerch.enabled = false; }
    55.  
    56.         }
    57.         else { futurePerch.enabled = false; }
    58.     }
    59. }
    60.  
     
  5. Jaklans

    Jaklans

    Joined:
    Sep 8, 2017
    Posts:
    2
    For the issue where the return position is (0,0,0), I found this was because the maps internal variable "InitialZoom" was not yet set to a non zero value. Because of this, it was multiplying all return values by 2^-16.

    The workaround I found was to handle my logic in the update function, and only run it after checking to make sure Map.InitialZoom != 0 (Which happens at some arbitrary point after the start function, moving the script execution order was not enough to fix)
     
    Nephtis, tpaslou and ACazan like this.
  6. Inclusivetech

    Inclusivetech

    Joined:
    Jan 13, 2022
    Posts:
    4
    The reason this happens is because the map is still initializing when you're trying to convert the vector.
    The AbstractMap has an OnInitialized event that can be used to trigger stuff that needs to happen only after the map has been drawn.