Search code examples
jqueryajaxgoogle-mapsgeocode

How to call google map geocode service with jquery ajax


I'm trying to get the LatLng from google map geocode api. my code is below with address values is "New York, NY"

$.ajax({
    url: 'http://maps.googleapis.com/maps/api/geocode/json',
    data: {
        sensor: false,
        address: address
    },
    success: function (data) {
        alert(data)
    }
});

But I'm not getting any values in alert.

Thanks for any help.


Solution

  • You will need to contact the API regarding the rules.

    $.getJSON( {
        url  : 'https://maps.googleapis.com/maps/api/geocode/json',
        data : {
            sensor  : false,
            address : address
        },
        success : function( data, textStatus ) {
            console.log( textStatus, data );
        }
    } );
    

    Edit:

    How dumb of me, shouldn't be on stackoverflow in the morning! The problem is a cross-domain request. For security reasons this is not allowed. See: How to make cross-domain AJAX calls to Google Maps API?

    Please use Google's own Geocoding client.