2014-03-06 214 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); 
});