0

不會被解僱,我使用谷歌地圖API V3的JavaScript。我設法讓我的網頁上的地圖。現在我需要在用戶點擊地圖時執行反向地理編碼。對於我已經寫入的必要事件偵聽器和反向地理編碼代碼谷歌地圖API V3,單擊事件在Java腳本的

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=My_Key&sensor=false"></script> 
<script type="text/javascript"> 
    var map; 
    function initialize() { 
    var myLatlng = new google.maps.LatLng(-34.397, 150.644); 
    var mapOptions = { 
      zoom: 4, 
      center: myLatlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
    } 
    google.maps.event.addListener(map, 'click', function(event) { 
    placeMarker(event.latLng); 
    var myLatLng = event.latLng; 
    var lat = myLatLng.lat(); 
    var lng = myLatLng.lng(); 
    var latlng = new google.maps.LatLng(lat, lng); 

    //Code to reverse geocode follows 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     if (results[1]) { 
      map.setZoom(11); 
      marker = new google.maps.Marker({ 
       position: latlng, 
       map: map 
      }); 
      infowindow.setContent(results[1].formatted_address); 
      infowindow.open(map, marker); 
      document.forms["wheregoing"]["start"].value=results[1].formatted_address; 
     } 
    } else { 
     alert("Geocoder failed due to: " + status); 
    } 
    }); 
    }); 


    function placeMarker(location) { 
    var marker = new google.maps.Marker({ 
     position: location, 
     map: map 
    }); 
    map.setCenter(location); 
    } 

上面提到的代碼是爲Java腳本。在HTML我已宣佈其中地圖應顯示的地方如下

<div id="map_canvas"></div> 

我面臨的問題是,當用戶點擊地圖上的放置地標應放置在該點和地址應出現在在html表單中名爲「start」的文本框,但它不會發生。地圖正在網站上加載,但未檢測到點擊事件。 我正在本地主機上工作。我正在測試localhost上的這個map功能。請幫忙。告訴我哪裏出錯了,爲什麼它不檢測點擊事件。我在這

+0

我說你有一個競爭條件,即當添加偵聽器時,映射不會被初始化。我現在必須走了,所以我不能詳細回答,希望它能幫助你!也許吧'