2012-10-05 65 views
-1

試圖將標記添加到我創建的聯繫人頁面上的谷歌地圖。地圖渲染正常,但不顯示標記。我的繼承人代碼:標記未添加到我的地圖

function initialize() { 
        var latlng = new google.maps.LatLng(-43.51187,172.621192); 
        var myOptions = { 
         zoom: 15, 
         center: latlng, 
         mapTypeId: google.maps.MapTypeId.ROADMAP 
       }; 
       var map = new google.maps.Map(document.getElementById("map_canvas"), 
        myOptions); 
        NSMarker(); 
       } 

       // Function for adding a marker to the page. 
       function addMarker(location) { 
        marker = new google.maps.Marker({ 
         position: location, 
         map: map 
        }); 
       } 

       // Add Marker Location 
       function NSMarker() { 
        NSArchitects = new google.maps.LatLng(-43.51187,172.621192); 
        addMarker(NSArchitects); 
       } 

回答

4

聲明var map外初始化()函數

var map; // declare globel 

function initialize() { 

    var latlng = new google.maps.LatLng(-43.51187,172.621192); 
    var myOptions = { 
      zoom: 15, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
    myOptions); 
    NSMarker(); 

} 

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

      // Add Marker Location 
      function NSMarker() { 
       NSArchitects = new google.maps.LatLng(-43.51187,172.621192); 
       addMarker(NSArchitects); 
      } 
+0

謝謝,我知道我只要我看到了答案有杜時刻。 – JamiePatt