2013-12-12 84 views
-1

這是我的index.html文件 - 我遇到問題的部分是使地理編碼功能正常工作。窗體和按鈕顯示出來,但是當我按下按鈕時沒有任何反應。Google地理編碼器不起作用

<!DOCTYPE html> 
<html> 
<head> 
<title>First Google Map</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
    function initialize() { 
    var latlng = new google.maps.LatLng(43.696299,-79.346271); 
    var myOptions = { 
    zoom:13, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.MAP 
}; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    geocoder = new google.maps.Geocoder(); 

    var myPlacesKML = new google.maps.KmlLayer('http://mristo.webatu.com/melissaristo/kml/torontodonvalley.kml?ver=1'); 
    myPlacesKML.setMap(map); 

    function geocode() { 
    address = document.getElementById("address").value; 
    geocoder.geocode({ 
    'address': address, 
    'partialmatch': true}, geocodeResult); 
    } 
    } 
    </script> 
</head> 
<body onload="initialize()"> 
<input type="text" id="address"> 
<input type="button" onclick="geocode();" value="Geocode it!" 
} 
</script> 
<body onload="initialize()"> 
<div id="map_canvas" style="width:500px; height:400px"></div> 
</body> 
</html> 

窗體和按鈕顯示出來,但按鈕不會做任何事情,當我點擊它。 有什麼建議嗎?我對此非常陌生並且相當迷茫

+1

請正確縮進你的代碼,我不能告訴函數結束或它只是一個嵌套的東西 –

+0

@RUJordan希望這樣更好? – user3096351

+0

爲什麼在那個未封閉的輸入標籤之後有一個括號? –

回答

1

這段代碼有很多bug。我解決了它。它會在您輸入文本框中的地址處放置一個標記。

<!DOCTYPE html> 
<html> 
<head> 
<title>First Google Map</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
    var map; 
    function initialize() { 
    var latlng = new google.maps.LatLng(43.696299,-79.346271); 
    var myOptions = { 
    zoom:13, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.MAP 
}; 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    geocoder = new google.maps.Geocoder(); 

    var myPlacesKML = new google.maps.KmlLayer('http://mristo.webatu.com/melissaristo/kml/torontodonvalley.kml?ver=1'); 
    myPlacesKML.setMap(map); 
    } 

    function geocode() { 
    address = document.getElementById("address").value; 
    geocoder.geocode({ 
    'address': address, 
    'partialmatch': true}, geocodeResult); 
    } 

    function geocodeResult(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); 
    } 

    } 
    </script> 
</head> 
<body onload="initialize()"> 
<input type="text" id="address"> 
<input type="button" onclick="geocode();" value="Geocode it!"> 

<div id="map_canvas" style="width:500px; height:400px"></div> 
</body> 
</html> 

下面是錯誤: 1.額外}
2.地圖變量是局部的,是全球性的需要。
3.從內部缺失功能geocodeResult
4.額外的身體標記
5.缺少閉合>從輸入
6.移動地理編碼()初始化()到全球。

相關問題