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.
Enable Geocoding API In your Google Cloud Platform Console, go to APIs & Services → Dashboard → Enable APIs & Services at the top and choose Maps JavaScript API from the API Library. This will open up the Maps JavaScript API page and Enable it.
The Geocoding API uses a pay-as-you-go pricing model. Geocoding API requests generate calls to one of two SKUs depending on the type of request: basic or advanced. Along with the overall Google Terms of Use, there are usage limits specific to the Geocoding API.
You must include an API key with every Geocoding API request.
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.
there is no need to make use of jquery, Google maps javascript API v3 has build in its own functions which makes the API easy to use.
https://developers.google.com/maps/documentation/javascript/geocoding https://developers.google.com/maps/documentation/geocoding/intro
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function(results, status) {
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
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With