javascript - Get address coordinates of Google Maps API by ajax() request -


i'm trying lng , lat coordinates of google maps api next example http://jsbin.com/inepo3/7/edit. expect 'success' popup, keeps showing 'error' popup. google maps-request gives correct json feedback (checked 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 know how fix issue?

regards, guido lemmens

edit

i found bether solution using google maps javascript api combined in jquery:

<script type="text/javascript">     $().ready(function() {         var user1location = "amsterdam, netherlands";         var geocoder = new google.maps.geocoder();         //convert location longitude , 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> 

google map api v3 makes harder external libraries work jsonp. here blog post it.

jsonp , google maps api geocoder plus fix w/ jquery


an alternative way of getting geocoding use google map v3 api geocoder service. here example helped person having similar issue replace jsonp use google map v3 geocoder service. take @ jsfiddle demo:

this core. use twitter tweet's address (ie. london, madrid or georgia etc) , convert actual address latlng using google map's geocoder service:

$.getjson(     url1, function(results) { // tweets         var res1 = results.results[0].text;         var user1name = results.results[0].from_user;         var user1location = results.results[0].location;          // first tweet in response , place inside div         $("#last-tweet1").html(res1 + "<p>from: " + user1name + " (" + user1location + ")</p><p>");         //convert location longitude , 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>");         });     }); 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -