I'm trying to do a simple geocode function for a map tool. I get the geocode fine, but I'm hoping to pass the location object back as the return value for the geocode function.
Something like this:
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
console.dir(location);
return location;
} else {
return false;
}
});
}
the console.dir of the location item shows the expected location object, so the function is being called and is successfully returning data.
this function is called by another process, which would then build the markers.
开发者_StackOverflowif (coordinates = codeAddress(myaddress){
// do stuff
}
However, the coordinates
variable always evaluates as undefined, so the condition to "do stuff" is never met.
I know I'm probably missing something really obvious about the definition of the coordinates var, but I'm not sure what it is.
Thanks for help.
Basic code at: http://jsfiddle.net/2TXZ4/3/ though the map isn't getting drawn for whatever reason.
The geocode
method is asynchonous so you need to do any processing in the callback you are providing to the method or you can provide a callback to your codeAddress
method like this similar question/answer describes (How do I return a variable from Google Maps JavaScript geocoder callback?). In the code below, I moved what you were doing in the doStuff
function into the callback of the geocode
function - it should work, but I can't test it with the jfiddle link you provided (probably due to Google Maps API keys). I hope this helps!
function codeAddress(address) {
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
console.log(location);
map.setCenter(location);
var marker = new google.maps.Marker({
map: map,
position: location
});
} else {
alert("Geocode was not successful for " + address);
}
});
}
window.doStuff = function() {
var address = document.getElementById("address").value;
codeAddress(address);
}
if (coordinates = codeAddress(myaddress)
- that's an assignment, not a comparison.
Ok, here's a wild hack (that does work):
<script type="text/javascript"> var geocoder; var loc; var l = false;
function initialize() {
geocoder = new google.maps.Geocoder();
var mapDiv = document.getElementById('map_canvas');
map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}); }
function codeAddress(address) {
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
loc = results[0].geometry.location; l = true;
console.log(loc); //alert (loc);
//return loc; doStuff(loc)
} else {
return false;
}
}); }
function doStuff(geo_marker){ if(l==false){
var thisaddress = document.getElementById("address").value;
var myResult = codeAddress(thisaddress); }else{
if (loc) {
map.setCenter(loc);
var marker = new google.maps.Marker({
map: map,
position: loc
});
l = false; //important to use function more than once :))
} else {
alert("Geocode was not successful");
} }
};
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<body>
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" onclick="window.doStuff()">
</div>
<div id="map_canvas" style="height:600px; width: 500px; position: absolute;
top: 80px"> </div>
</body>
I do think it makes more sense to do geocoding and add marker to the map in one function though, to avoid this calling back and forth between the functions.
精彩评论