Uncaught SyntaxError trying to do reverse geocoding

356 views Asked by At

I'm trying to do some reverse geocoding, and I made a quick snippet to test out my code to see if it works.

This is the following code:

<html>
<style>
/**
* Default attributes for gadget body.
*/
body {
 font-family: Arial;
background: none transparent;
padding: 0px;
}
html, body, #map_canvas {
    margin: 0;
    padding: 0;
    height: 100%;
}
</style>
<body>
<div id="map_canvas"></div> 
<script>
var APIKey = "MyKeyValueIsInHere";   
var geocoder;
var map;
var marker;
var locationTest = 'http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1';
var lat = 55.653363;
var lng = 12.547604;

function initialize() {
    var latlng = new google.maps.LatLng(lat,lng);
    // set the options for zoom level and map type
    var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    // create a map in the map_canvas div element
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    map.setCenter(latlng);
            var marker = new google.maps.Marker({
                map: map, 
                position: latlng

            });
            alert(locationTest);
            var text = JSON.parse(locationTest);
            var infoWindow = new google.maps.InfoWindow();

            var houseNumber = text.address.house_number;
            var road = text.address.road;
            var suburb = text.address.suburb;
            var zipCode = text.address.postcode;
            var city = text.address.city;

            var address = road + " " + houseNumber + ", " + zipCode + " " + suburb + " i " + city;


            google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(address);
            infoWindow.open(map, this);
            });
}
function loadScript() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/js?key=" + APIKey + "&sensor=false&callback=initialize";  

    document.body.appendChild(script);
}   

loadScript();   
</script>
</body>
</html>

Anyhow, as i run it, i get an alert popping saying : http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1

Bascially, what I want to do is pop an infowindow on top of my marker on my google maps. And the infowindow should have the text that is the variable address.

But as i run this code, i get an

Uncaught SyntaxError: Unexpected token h

on line 45 which is this line:

var text = JSON.parse(locationTest);

So does anyone have an idea about how i fix this problem?

Thanks in advance!

1

There are 1 answers

8
emrys57 On BEST ANSWER

You have

var locationTest = 'http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1';

and then

var text = JSON.parse(locationTest);

but JSON.parse expects its parameter to be a JSON-encoded string: documentation. You need first to get the contents of the webpage as a string, and then parse it as JSON. The error message is complaining about the "h" at the front of the "http". It expects the first character to be "{".

After your alert statement, you could try:

var $e = $('<div></div>'); // a jQuery object that does not affect your displayed page
$e.load('http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1', function (response) {
    // This code only runs once the response is available from openstreetmap.org
    var text = JSON.parse(response);
    var infoWindow = new google.maps.InfoWindow();
    var houseNumber = text.address.house_number;
    var road = text.address.road;
    var suburb = text.address.suburb;
    var zipCode = text.address.postcode;
    var city = text.address.city;
    var address = road + " " + houseNumber + ", " + zipCode + " " + suburb + " i " + city;
    google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(address);
        infoWindow.open(map, this);
    });
});