Author Topic: Lat/Lon to XYZ Coordinates  (Read 58 times)

Mike O'Donnell

  • Guest
Lat/Lon to XYZ Coordinates
« on: July 01, 2019, 08:17:00 AM »
Hey guys. So I'm using the Word Political Map, and it's working great so far. I'm trying to implement a system where the user can type latitude and longitude into text fields (Such as 36.2048, 138.2529 for Japan)and press a button, which will spawn a game object at that lat/lon location (converted to XYZ). Problem is, the object doesn't appear anywhere near where it should. Here's what I've got.
Code
latitude = float.Parse(latField.text);
        longitude = float.Parse(lonField.text);
        x = 0.5f * Mathf.Cos(latitude) * Mathf.Cos(longitude);
        y = 0.5f * Mathf.Cos(latitude) * Mathf.Sin(longitude);
        z = 0.5f * Mathf.Sin(latitude);
        Instantiate(plane, new Vector3(x, y, z), Quaternion.Euler(0, 0, 90f));
Any ideas or suggestions would be appreciated.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4736
    • View Profile
Re: Lat/Lon to XYZ Coordinates
« Reply #1 on: July 01, 2019, 08:44:33 AM »
Hi Mike,

You can just use the Conversion.GetSpherePointFromLatLon() static method which accepts a Vector2 (x = lat, y = lon) and returns a Vector3 with the correct position (in local coordinates to the sphere).
Or you can also use the Calculator component (map.calc....) component for the same task (although the Conversion static class is the new preferred way).

Regard


Mike O'Donnell

  • Guest
Re: Lat/Lon to XYZ Coordinates
« Reply #2 on: July 01, 2019, 09:07:56 AM »
Ok, that hits closer to the mark, thanks. One issue though is that I'm not sure how to specify direction. The GetSpherePointFromLatLon takes in lat and lon as floats. Is there any way to specify a direction (N, S, E, W) when converting?

Thanks,
Mike

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4736
    • View Profile
Re: Lat/Lon to XYZ Coordinates
« Reply #3 on: July 01, 2019, 09:47:58 AM »
North and East are positive.
South/West negative.

Mike O'Donnell

  • Guest
Re: Lat/Lon to XYZ Coordinates
« Reply #4 on: July 01, 2019, 10:57:33 AM »
Fantastic, thank you.