Get the address coordinates of the Google Maps API for ajax ()

I am trying to get the lng and lat coordinates of the Google Maps API in the following http://jsbin.com/inepo3/7/edit example. I expect a success popup, but it continues to show an Error popup. Google maps-request gives correct json feedback (checked by firebug).

<script type="text/javascript"> $().ready(function() { $.fn.getCoordinates=function(address){ $.ajax( { type : "GET", url: "http://maps.google.com/maps/api/geocode/json", dataType: "jsonp", data: { address: address, sensor: "true" }, success: function(data) { set = data; alert(set); }, error : function() { alert("Error."); } }); }; $().getCoordinates("Amsterdam, Netherlands"); }); </script> 

Does anyone know how to fix this problem?

Regards, Guido Lemmens

EDIT

I found a solution using the Google Maps Javascript API integrated in jQuery:

 <script type="text/javascript"> $().ready(function() { var user1Location = "Amsterdam, Netherlands"; var geocoder = new google.maps.Geocoder(); //convert location into longitude and latitude geocoder.geocode({ address: user1Location }, function(locResult) { console.log(locResult); var lat1 = locResult[0].geometry.location.lat(); var lng1 = locResult[0].geometry.location.lng(); $("#testDiv").html("latitude:" + lat1 + "<p>longitude:" + lng1 + "</p>"); }); }); </script> 
+7
source share
1 answer

Google Map API V3 makes it difficult for external libraries to work with JSONP. Here is a blog post about it.

JSONP and Google Maps API Geocoder Plus A Fix w / jQuery


An alternative way to get geocoding is to use the Google Map V3 API Geocoder Service . Here is an example that I helped a person who had a similar problem replacing his JSONP with the Google Map V3 Geocoder Service. Take a look at the JSFiddle Demo:

This is basically the core. We mainly use twitter to get the tweet address (IE, London, Madrid or Georgia, etc.) and convert the actual address to LatLng using the Google Map Geocoder service:

 $.getJSON( url1, function(results) { // get the tweets var res1 = results.results[0].text; var user1name = results.results[0].from_user; var user1Location = results.results[0].location; // get the first tweet in the response and place it inside the div $("#last-tweet1").html(res1 + "<p>from: " + user1name + " (" + user1Location + ")</p><p>"); //convert location into longitude and latitude geocoder.geocode({ address: user1Location }, function(locResult) { console.log(locResult); var lat1 = locResult[0].geometry.location.lat(); var lng1 = locResult[0].geometry.location.lng(); $("#testDiv").html("latitude:" + lat1 + "<p>longitude:" + lng1 + "</p>"); }); }); 
+9
source

All Articles