JSON

Obtaining JSON Content from a Service
need to get Google API key
add CORS Toggle extension to Chrome
    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>JSON</title>
        <script src="key.js"></script>
        <script>
            <!-- https://mapsgoogleapis.com/maps/api/place/nearbysearch/json?location=[0],[1}&redius=500&types=food&name=cruise&key= -->
            var xmlhttp;
            window.onload = function () {
                navigator.geolocation.getCurrentPosition(success, fail);
            }
            function success(position) {
                var lat = position.coords.latitude;
                var lon = position.coords.longitude;
                getPositionInfo(lat, lon);
            }
            function getPositionInfo(lat, lon) {
                var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + lat + "," + lon + "&radius=500&types=food&key=" + key;
                xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = processPlaces;
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
            }
            function processPlaces() {
                if (xmlhttp.readyState == 4) {
                    var jsonResponse = JSON.parse(xmlhttp.responseText);
                    console.log(jsonResponse);
               }
            }
            function fail(e) {
                alert(e.message);
            }
        </script>
    </head>
    <body>
    </body>
    </html>
        
Top

Index

Parsing JSON Content
    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>JSON</title>
        <script src="key.js"></script>
        <script>
            <!--    https://mapsgoogleapis.com/maps/api/place/nearbysearch/json?location=[0],[1}&redius=500&types=food&name=cruise&key= -->
            var xmlhttp;
            window.onload = function () {
                navigator.geolocation.getCurrentPosition(success, fail);
            }
            function success(position) {
                var lat = position.coords.latitude;
                var lon = position.coords.longitude;
                getPositionInfo(lat, lon);
            }
            function getPositionInfo(lat, lon) {
                var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + lat +
                    "," + lon + "&radius=1500&types=food&key=" + key;
                xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = processPlaces;
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
            }
            function processPlaces() {
                var out = "";
                if (xmlhttp.readyState == 4) {
                    var jsonResponse = JSON.parse(xmlhttp.responseText);
                    var places = jsonResponse.results;
                    for (x = 0; x < places.length; x++) {
                        var name = places[x].name;
                        var address = places[x].vicinity;
                        var rating = places[x].rating == null ? "not rated" : places[x].rating;
                        out += "<h2>" + name + "</h2>" + address + " : " + rating + "<br/>";
                    }
                }
                document.getElementById('result').innerHTML = out;
            }
            function fail(e) {
                alert(e.message);
            }
        </script>
    </head>
    <body>
        <div id="result">
        </div>
    </body>
    </html>
        
Top

Index

n4jvp.com