Google地图标记未显示

时间:2014-03-06 08:02:54

标签: javascript google-maps-api-3

我似乎已经搜索了每个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有没有人知道为什么这可能不起作用?

由于

1 个答案:

答案 0 :(得分:0)

此代码

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

i的值=最大位置长度,您可以将i保存到标记的标签

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);
});