我是新的谷歌地图API。我花了一些样品从手动,并试图去改变它,我需要的方式,所以这里是我想做的事: 这是谷歌的例子映射手动Noob问题:如何获取信息,如果谷歌地图加载(初始化)
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(40.714353, -74.005973);
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
我已经改变codeAddress功能小并试图把它在页面加载这样的:
function codeAddress(address) {
//var address = document.getElementById("address").value;
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
codeAddress("Some Address");
但它给我一个JavaScript错误“地理编码器未定义”。我如何让脚本等到地图加载后再运行codeAddress函数?我不想在初始化期间路由我的地址。
我希望这是明确的 预先感谢您。
我很抱歉这个愚蠢的问题。我有地址字符串的问题,所以我纠正它,并得到脚本工作。无论如何感谢 – KoKo 2010-11-17 16:49:21