开发者

Generate Sunrise and Sunset Times from Latitude Longitude on today's date

开发者 https://www.devze.com 2023-02-24 06:23 出处:网络
Everyone. I\'m doing a project that makes only use of JavaScript (client-side, not server-side) and I want to use the script from NOAA that I found in the other thread (I can\'t list more than one lin

Everyone. I'm doing a project that makes only use of JavaScript (client-side, not server-side) and I want to use the script from NOAA that I found in the other thread (I can't list more than one link as a new user, though) and I found this:

http://www.srrb.noaa.gov/highlights/sunrise/sunrise.html

What I am trying to do is that when a user enters the latitude and longitude and clicks "Submit, " it will get the today's sunrise/sunset data and store them in two variables (sunrise and sunset, as an example).

I really wish, however, that they could have put all teh scripts (except the scripts that embeds in combo boxes) in a separate JavaScript file so that I can provide the source of JavaScript code from NOAA's website into my project that I've been working on.

                <div id="locationSetupDiv">
                    <fieldset>
                        <legend>Location</legend>
           开发者_运维技巧             <p>
                        <a href="javascript:var load = window.open('http://www.esrl.noaa.gov/gmd/grad/solcalc/')">
                            Get your longitude and latitude from here.</a>
                        </p>
                        <table class="inputData">
                            <tr>
                                <th style="width: 75px;">
                                    <label id="labelLongitude" for="inputModuleIDNumber">
                                        Longitude:</label></th>
                                <td><input id="inputLongitude" /></td>
                            </tr>
                            <tr>
                                <th><label id="labelLatitude" for="inputModuleName">
                                    Latitude:</label></th>
                                <td><input id="inputLatitude" /></td>
                            </tr>
                        </table>
                        <input type="button" value="Submit Changes" 
                               style="float: right; margin-top: 1em;" />
                    </fieldset>
                </div>

The above code is in HTML. Now, here's my JavaScript code:

function PopulateFromXML()
{
    listModules = document.getElementById(
        "listModules").getElementsByTagName("tbody");

    // Gather the text fields for location data.
    inputLongitude = document.getElementById("inputLongitude")
    inputLatitude = document.getElementById("inputLatitude")

    // Firefox's DOM Parser treats whitespaces as text nodes, including
    // line breaks.
    removeWhitespace(xmlDoc.documentElement);

    // Send the document's root element to the XML Object variable.
        xmlObj = xmlDoc.documentElement;

    // Perform the checks and populate the tables
    // and any other elements that are needed.
    if (xmlObj.tagName == "HomeAutomationInterface")
    {
        // Check to be sure the first node is the "Setup" node. It contains the
        // location node.
        if ((xmlObj.childNodes[0].tagName == "Setup") &&
            (xmlObj.childNodes[0].childNodes[0].tagName == "Location"))
        {
            // Copy the data from one of the attributes to the respective text boxes.
            inputLongitude.value = xmlObj.childNodes[0]
                .childNodes[0].getAttribute("longitude");
            inputLatitude.value = xmlObj.childNodes[0]
                .childNodes[0].getAttribute("latitude");
        }
        // The second node within the root element is Modules node.
        // This will be implemented.
        if (xmlObj.childNodes[1].tagName == "Modules")
        {
            ListModulesInTabularData();
       }
        // The third node within the root element is Scenes node.
        // You need modules in order for scenes to work.
        // This will be implemented.
        if (xmlObj.childNodes[2].tagName == "Scenes")
        {
            ListScenesInTabularData();
        }
        // The last element is the Timers node.
        // This will either activate the scene or turn on, off,
        // dim, brighten, or set the light level of the module if
        // it is the light module or turn on or off the appliance
        // module. This will be implemented.
        if (xmlObj.childNodes[3].tagName == "Timers")
        {
            ListTimersInTabularData();
        }
    }
}

As you can see, the two text boxes inside a fieldset gets written when populated from the XML file. Although I'm doing a project as a proof-of-concept, I can't find a way to save all the data to an XML file, so my teacher told me not to worry about that. But since I've written a code to dynamically populate all the XML data in a tabular format, I've been looking for a particular JavaScript code in the Internet or maybe a JavaScript file that I can link to from my project, but with the JavaScript code that I've borrowed from NOAA for my personal project, does anyone know how do I get the sunrise/sunset times according to the latitude, longitude, and today's date?

Update

It looks like I found something in the Internet (I did a Google search for: sunrise sunset web service):

http://www.earthtools.org/webservices.htm

I did a search using keywords "sunrise sunset JavaScript" (without quotes) and it took me a while to search but then with "JavaScript" replaced with "web service, " I am able to find a website I'm looking for, but then I really don't think this will work with XMLHttpRequest when it comes to "same origin policy" in IE8/9 that is suppose to prevent cross-site scripting.


I've created a javascript calculator for sunrise and sunset and put it on github here. It's based on the calculation published by the National Renewable Energy Laboratory. I've tried to make it reasonably easy to use, but you're welcome to submit improvements.

0

精彩评论

暂无评论...
验证码 换一张
取 消