A way to add a callback to Geocoding JSON response on Google Maps API V3
So I have a URL like:
http://maps.googleapis.com/maps/api/geocode/json?&sensor=false&address=1330+SW%203rd%20Ave,%20Portland,%20OR
But it returns just JSON. Im looking to do a JSONP call. I want to do this without server side code. Is there a way to add a var name to it or a function? Normally APIs have a callback
param or something. Or, do any of you know any hacks? My code right now is:
createScriptT开发者_如何学Pythonag:function(the_src,callback){
var headID = document.getElementsByTagName("head")[0],
newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.onload=callback;
newScript.src = the_src;
headID.appendChild(newScript);
}
//Then call it like:
createScriptTag(
'http://maps.googleapis.com/maps/api/geocode/json?&sensor=false&address=1330+SW%203rd%20Ave,%20Portland,%20OR',
function(){ /* I can't do anything with the JSON :( */ }
)
Looks to me like you're using the Geocoder API rather than the Maps API. According to the documentation the Geocoder API is not designed for real time use:
http://code.google.com/apis/maps/documentation/geocoding/
This service is generally designed for geocoding static (known in advance) addresses for placement of application content on a map; this service is not designed to respond in real time to user input, for example. For dynamic geocoding (for example, within a user interface element), consult the documentation for the JavaScript API V2 Client Geocoder, the JavaScript API V3 Client Geocoder, or the Maps API for Flash Client Geocoder.
Perhaps you need to look at http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding which is client side with built in callbacks. Although I believe the licence on that API requires the geocode result to be used in conjunction with a google map.
精彩评论