好的,所以用户选择国家,然后通过自动完成小部件,他们可以选择他们居住的地区,城市和地区。他们选择的值被连接成一个地址,用于调用谷歌地图API并显示一个标记地址的地图......不幸的是,这不起作用......我在Firebug中得到了这个例外:
未被捕的例外:[例外...... “组件返回失败代码: 0x80004005(NS_ERROR_FAILURE) [nsIDOMViewCSS.getComputedStyle]” nsresult:“0x80004005 (NS_ERROR_FAILURE)“位置:”JS frame :: http://maps.gstatic.com/intl/en_us/mapfiles/api-3/4/11a/main.js :: Xk ::第55行“数据:否]
这是我的代码:
包含此src="http://maps.google.com/maps/api/js?sensor=false"
var address = selectedArea + ', ' + selectedCity + ', ' + selectedDistrict + ', Lebanon';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var map = new google.maps.Map($("#addressMap"));
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);
}
});
那是怎么回事?
答案 0 :(得分:22)
创建地图对象时,您会提供一个jQuery对象,但它除了DOM对象之外。尝试DOM对象本身。
我还添加了一些选项,缩放级别和地图类型。
以下是最终代码:
var address = selectedArea + ', ' + selectedCity + ', ' + selectedDistrict + ', Lebanon';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var mapOptions = { zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP };
var map = new google.maps.Map(document.getElementById('addressMap'), mapOptions);
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);
}
});