Aug
9
2011

Latitude Longitude on 3D sphere

Testing sphere (earth), with latitude longitude positions.
(Texture is just thrown in there and sphere is rotated 197′ to get roughly correct positions,
tested atleast few cities)

Maybe could use this with GeoIP, News location plotting, Weather news plotter,
GPS data, Twitter, GoogleMaps api etc..?

*Code for the math stuff is taken from here:
http://www.actionscript.org/forums/s…957#post722957
Texture from nasa images (said to be public domain / no copyright)

Webplayer:
http://unitycoder.com/upload/demos/latlong1/
Demo location is set at London (lat 51.50015, long -0.1262362)
(mouse orbit)

Download source + webplayer scene:
latlong_1.unitypackage


About the Author:

.fi

20 Comments + Add Comment

  • convert google maps lat/lng to pixel coordinates and vice versa:
    https://gist.github.com/flarb/4980598

  • Hi, I’ve just tested your project in Unity 5 and the marker seems to be a bit off – it seems to put it in somewhere near Romania. Do you know if something has changed? I’ve been trying to get the markers to match the correct location, but am having a tough time.

    • Most likely the model is just wrong (its default unity sphere, with the default UV mapping which breaks at top and bottom), cant remember if I had to manually adjust the material UV starting position to match London horizontally..

      • Yeah, I have tried playing with the material and getting it into the right position, but no matter what, my two points (Newcastle, UK; London, UK) seem to always end up in France!

        It’s driving me mad.

        This is what I am using:

        //Radius is 150… It is half the Scale of my sphere which is 300
        private Vector3 convertSphericalToCartesian(float lat, float lon, float radius)
        {

        //Convert to radians first
        lat = NormalizeRadian(DegreeToRadian (lat));
        lon = DegreeToRadian (lon);

        float x = radius * Mathf.Sin(lat) * Mathf.Cos(lon);
        float z = radius * Mathf.Sin(lat) * Mathf.Sin(lon);
        float y = radius * Mathf.Cos(lat);

        return new Vector3(x,y,z);
        }

        private float DegreeToRadian(float input){

        float output = 0.0f;
        output = Mathf.PI * input / 180;

        return output;
        }

        private float NormalizeRadian(float radian){
        return radian – 1.570795765134f;
        }

        Any ideas?

        • tested with 4.6x and 5.1, and the sphere is different in 5.1 (texture is clearly in a different position), so that breaks the old demo at least..

          • Hey did you figure how to position the sphere so that it lines up correctly? or must i just make a couple of tests and get a the highest accuracy by looking? if i can get the position of the seem then it should work out.

            anyway thanks for the example, at least i know in what direction to go

      • no, havent made further tests.. in the old version i think it was just a matter of manually rotating the sphere until most locations match, but that wont be accurate of course..

        • hey mGear

          i finally got it right. i basically had to just give my sphere a y rotation of 180 and fix the uv mapping on it. oh and i had to convert the code to playmaker, this was a bitch to do.

          anyway rock on!

          • How did you fix the UV mapping?

  • Hi, Thanks very much I tried your project and it works great. How ever in my project I want to use a cylinder instead of an sphere. I wanted to ask if you know any ways to also change the rotation of the cylinder so it always stand straight on the earth sphere.
    Thanks very much

    • not sure if I understood, maybe image would help.. 🙂

      • Thanks, I was over complicating it, a simple lookAt solved my problem 🙂

  • Works great , Thanks buddy

  • Hi there, this is super useful but running your current demo in unity, the placement is not in the right place, has unity 5 broken something?

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.