2014-03-06 94 views
-1

我似乎搜索了每個SO問題和谷歌地圖API,但無法看到爲什麼我的標記沒有顯示在我的地圖上。我幾乎從其他地方獲取了代碼,並且控制檯沒有顯示任何錯誤。這是我的代碼谷歌地圖標記沒有顯示

<script type="text/javascript">  
     //declare namespace 
     var up206b = {}; 

     //declare map 
     var map; 

//set the geocoder 
var geocoder = new google.maps.Geocoder(); 

     function trace(message) 
     { 
      if (typeof console != 'undefined') 
      { 
       console.log(message); 
      } 
     } 

     //Function that gets run when the document loads 
     up206b.initialize = function() 
     { 
      var latlng = new google.maps.LatLng(-31.954465, 115.859586); 
      var myOptions = { 
       zoom: 13, 
       center: latlng, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

       // Buggles marker location 

      var locations = [ 
       ['Test 1', -31.9522, 115.8589], 
       ['Test 2', -31.1522, 115.4589], 
       ['Test 3', -31.9322, 115.1589], 
       ['Test 4', -31.5522, 115.9589], 
       ['Test 5', -32.9522, 115.2589] 
      ]; 

       var infowindow = new google.maps.InfoWindow(); 

       var marker, i; 

       for (i = 0; i < locations.length; i++) { 
        marker = new google.maps.Marker({ 
         position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
         map: map 
        }); 

        google.maps.event.addListener(marker, 'click', (function (marker, i) { 
         return function() { 
          infowindow.setContent(locations[i][0]); 
          infowindow.open(map, marker); 
         } 
        })(marker, i)); 
       } 

     } 


     //geocode function 
     up206b.geocode = function() 
     { 
      var address = $('#location').val(); 
      geocoder.geocode({ 'address': address}, function(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 
        }); 
         $('#buggles-locations').removeClass('hidden'); 

       } 
       else 
       { 
        alert("Geocode was not successful for the following reason: " + status); 
       } 
      }); 
     } 


    </script> 

,我跟着的例子包括在本體內加載功能

<body onload="up206b.initialize()"> 

的例子是在這裏http://buggles.crushme.com.au/index.php沒有人有任何想法,爲什麼這可能無法正常工作?我的

感謝

+0

我也嘗試只需添加一個標記使用一個非常基本的方法,但仍然不會顯示,所以我假設我在一個非常基本的層次上做了錯誤的事情,但是我不擅長JavaScript,所以看不到它。 –

+0

你的共享鏈接中的地圖在哪裏? – alalp

+0

對不起,它位於主圖像滑塊下方,它在您輸入地址進行搜索時會滑入,但對於開發,我只是爲了便於調試而將其取消。我已經徹底改變了我的代碼,並獲得了標記,但沒有進行地理編碼,只是試圖將它們結合起來。 –

回答

0

這個代碼

值=的位置最大長度,可以節省我標記標記的

marker = new google.maps.Marker({ 
     position : myLatLng, 
     map : map, 
     title: i.toString(),    
    }); 
google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent(partInt(locations[this.getTitle())][0]); 
     infowindow.open(map, this); 
});