Answer jQuery and google maps json

I'm having problems getting location information from google api maps

the code is pretty straightforward

$.ajax({
    type: "GET",
    cache: false,
    url: "http://maps.googleapis.com/maps/api/geocode/json",
    dataType: "jsonp",
    data: {
        address: "Ljubljana " + "Slovenia",
        sensor: "false"
    },
    jsonpCallback:'json_response',
    success: function(data) {
        top.console.debug(data);
        $('#location_setter').dialog('close');
    },
    error: function() {
        alert("Error.");
    }
});


function json_response(data){
    alert("works");
}

I always get an error message. I also tried directly (I read somewhere that the callback should be set at the end ...

$.ajax({
    type: "GET",
    cache: true,
    url: "http://maps.googleapis.com/maps/api/geocode/json?address=Ljubljana Slovenia&sensor=false",
    dataType: "jsonp",
    jsonpCallback:'json_response',
    success: function(data) {
        top.console.debug(data);
        $('#location_setter').dialog('close');
    },
    error: function() {
        alert("Error.");
    }
});

correctly formed request URL:

http://maps.googleapis.com/maps/api/geocode/json?address=Ljubljana%20Slovenia&sensor=false&callback=json_response

and he gives me the correct json

please inform!

You can "play" with him at http://jsfiddle.net/PNad9/

+5
source share
4 answers

-... , , , :

google maps js

var script = document.createElement( 'script' );
        script.type = 'text/javascript';
        script.src = 'http://maps.google.com/maps/api/js?sensor=false&callback=get_longlat';
        $("body").append( script );

, get_longlat...

google

function get_longlat(address){

var geocoder = new google.maps.Geocoder();

if(!address){
    var p = US.get("location_postal");
    var c = US.get("location_city");
    var a = US.get("location_address");
    var address = a + ', ' + p + ' ' + c + ', Slovenia';
}

if (geocoder) {
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            US.set("location_lat", results[0].geometry.location.lat(), 60);
            US.set("location_lon", results[0].geometry.location.lng(), 60);

            $('#location_setter').dialog('close');
        } 
        else {
            console.log('No results found: ' + status);
        }
    });
}

}

US -

, -

+2

, . URL-, google (http://maps.googleapis.com/maps/api/geocode/json), jsonp, JSON. Google JSON .

, , , , URL-: http://maps.google.com/maps/geo API Google. , .

jsfiddle ( , API Google Maps): < 2 >

- , , , Google Geocoder .

+1

, , . , , , , alert (data.status), 200. , , v2?!

error: function(data) {
    alert(data.status);
}

, , jsonp, , , , querystring? jsonpcallback json, 0.

, .

0
function codeAddress(address, title, imageURL) {
            console.log(address);

            geocoder.geocode({'address': address}, function (results, status) {
                console.log(results);
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);

                        var marker = new google.maps.Marker({
                            map: map,
                            position: results[0].geometry.location,
                            icon: "",
                            title: title
                        });

                    /* Set onclick popup */
                    var infowindow = new google.maps.InfoWindow({content: title});
                    google.maps.event.addListener(marker, 'click', function () {
                        infowindow.open(marker.get('map'), marker);
                    });
                }
                else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                    setTimeout(function () {
                        codeAddress(address, title, imageURL);
                    }, 250);
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
0

All Articles