0
我一直在嘗試將地址代碼到LatLng座標中,但我在這樣做時遇到了很多困難。我知道geocode()
是一個異步函數,我一直玩弄回調沒有用。我一直在尋找像這樣的其他帖子(How to return value from an asynchronous callback function?),但我很難理解這個概念。如果任何人都可以告訴我如何正確地執行回調函數,將不勝感激。很多頭痛在這一個。能夠得到回調做alert()
給出正確的LatLng,但沒有設置位置參數。抱歉,無法從前面引用的例子中弄清楚。相當新的語言。任何你可以指出我理解這個概念的參考資料也是非常值得讚賞的。謝謝!使用google.maps.Geocoder()。異步回調
var location;
var image;
var map;
var geocoder;
var address;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(42.763146, -73.3776),
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map);
addressSet(function(address) {
console.log(address);
// tried to set location = address;, location = new google.maps.LatLng(address);
// neither works
});
addMtsToMap(location,
map,
new google.maps.MarkerImage("img/mts/skier.png", null, null, null, new google.maps.Size(50,50)));
}
function addMtsToMap(location, map, image) {
var marker = new google.maps.Marker({
position: location,
map: map,
icon: image
});
var infoWindow = new google.maps.InfoWindow({content: "<h1>Powder!!!</h1>"});
google.maps.event.addListener(marker, "click", function() {
infoWindow.open(map, marker);
});
}
function addressSet(callback) {
geocoder = new google.maps.Geocoder();
geocoder.geocode({address: "Killington+VT"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
address = results[0].geometry.location;
callback(address);
}
});
}