Search code examples
javascriptgoogle-mapsmathcoordinatesastronomy

Project sun path onto Google maps for given observer position


I have the following list of details

Latitude and Longitude,
Date/Time
Altitude and Azimuth

What I'm trying to do is calculate a relative latitude and longitude to the one used in my calculation so I can draw an oval with Google Maps V3 (JavaScript)

Is this possible or should I convert my lat/lng to page pixels and do it that way (but this isn't how I want to achieve the task)

[EDIT]

I'm trying to gather latitude and longitude coordinates based on the altitude and azimuth as viewed by a single observer, so one single point on my map I want to visualise the path of the sun throughout the day/time.


Solution

  • Update: after chatting with Dave, I believe the following method will yield an accurate projection of a sun position onto a map.

    Here's a quick sketch of what the projection looks like. It's (supposed to be) 3D. We want to project the sun down onto a 2D surface where x and y are our offsets for the observer, and z is the distance to the projected sun:

    sun projection diagram

    Known:

    • distance from earth to sun, S = 149,597,870,700 m
    • phi (sun azimuth)
    • theta (sun altitude)

    What we really want is z (distance) and phi, the bearing. phi is known, so we need z. Keep in mind that S is gigantic so we should scale it down to a smaller number so our coordinates can be seen on the map. As long as we keep our new S constant, this shouldn't matter. Now, to find z:

    sin(theta) = z / S, therefore z = S * sin(theta)
    

    Once we have distance z and bearing phi, we can use these equations from movable-type to calculate a lat/lng coordinate:

    var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
              Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
    
    var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                     Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
    

    where d = distance traveled = our z and R = earth's radius = 6371000 (meters)

    That was easy! Now just iterate over all your sun positions and draw them on the map.

    Here are some quick results, using S=1000:

    Azimuth    altitude    z
    110        5           87
    150        20          342
    180        70          939
    210        20          342
    

    Note that if you're dealing with very large distances this will not be entirely accurate due to the difficulties of modeling the Earth.