我將下面的腳本放置在頁面的HEAD中。加載頁面時,地圖應該初始化。這個謎題有兩個部分,一個是document.ready中的腳本,它設置所有變量並配置我想要放置在頁面上的地圖。第二部分是啓動地圖的window.onload=initialize_map;
。javascript函數沒有運行
我相信一切都運行正常,但是,我不確定。我所知道的是,initialize_map函數永遠不會運行。我甚至試圖用initialize_map();
設置一個onclick按鈕來嘗試手動啓動地圖,但它仍然不起作用。我的代碼有問題嗎?任何幫助是極大的讚賞。
謝謝!
CODE題:
<script src= "http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAhTrgZ5jvdqcEQouEpPcZ_hS81NmJwGXlxuJr8lEEo4Njw3WRVhT8auzZb55JSMDkwIaCdNkPHL5gNg" type="text/javascript"> </script>
<script type="text/javascript">
$(document).ready(function(){
var dealerName = $('.name', '.adr').text();
var customerName = dealerName.slice(0, - 1);
var customerAddress = $('.street', '.adr').text() + ', ' + $('.locality', '.adr').text() + ', ' + $('.state', '.adr').text() + ', ' + $('.zipCode', '.adr').text();
$("#nameAddress .placeholderName").html(customerName);
$("#nameAddress .placeholderAddress").html(customerAddress);
var error_address_empty = 'Please enter a valid address first.';
var error_invalid_address = 'This address is invalid. Make sure to enter your street number and city as well?';
var error_google_error = 'There was a problem processing your request, please try again.';
var error_no_map_info = 'Sorry! Map information is not available for this address.';
var default_address = customerAddress;
var current_address = null;
var map = null;
var geocoder = null;
var gdir = null;
var map_compatible = false;
if(GBrowserIsCompatible()) {
map_compatible = true;
}
function initialize_map() {
if(map_compatible) {
map = new GMap2(document.getElementById('map_canvas'));
geocoder = new GClientGeocoder();
show_address(default_address);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
}
}
function show_address(address) {
if(map_compatible && geocoder) {
current_address = address;
geocoder.getLatLng(
address,
function(point) {
if(!point) {
alert(error_no_map_info);
} else {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml("<span style='font-size:14px; font-weight:bold;'>" + customerName + "<br /></span><span style='font-size:12px;'>" + address + "</span>");
}
}
);
}
return false;
}
function get_directions() {
if(map_compatible) {
if(document.direction_form.from_address.value == '') {
alert(error_address_empty);
return false;
}
document.getElementById('directions').innerHTML = '';
gdir = new GDirections(map, document.getElementById('directions'));
GEvent.addListener(gdir, 'error', handleErrors);
set_directions(document.direction_form.from_address.value, current_address);
}
return false;
}
function set_directions(fromAddress, toAddress) {
gdir.load("from: " + fromAddress + " to: " + toAddress,
{ "locale": "en" });
}
function handleErrors(){
if(gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert(error_invalid_address);
else if(gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert(error_google_error);
else if(gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert(error_address_empty);
else
alert(error_invalid_address);
}
});
window.onload=initialize_map;
</script>
感謝編輯....我只是工作這一點。 – RyanPitts 2010-09-17 19:16:17