谷歌地图信息窗口不起作用?

时间:2015-09-18 17:10:05

标签: javascript angularjs google-maps

我在这里附上了我的代码我正在努力将值传递给信息窗口我得到了Uncaught TypeError:无法读取属性' 0'未定义的。 期望: 我想在infowindow中显示值 2.当我点击按钮时,它应该传递id值 我添加了小提琴 http://jsfiddle.net/cLADs/135/

<!DOCTYPE html>
<html>
<head>

      <script type='text/javascript' src="https://maps.googleapis.com/maps/api/js?key=&v=3.0&sensor=true&language=ee"></script>


  <style type='text/css'>
    #map-canvas {
    width: 500px;
    height: 500px;
}
  </style>




<script type='text/javascript'>//<![CDATA[
window.onload=function($scope,$compile){
var gmarkers1 = [];
var markers1 = [];
var infowindow = new google.maps.InfoWindow({
    content: ''
});

// Our markers
markers1 = [
    ['0', 'Madivala', 12.914494, 77.560381, 'car','as12'],
    ['1', 'Majestic', 12.961229, 77.559281, 'third','as13'],
    ['2', 'Ecity', 12.92489905, 77.56070772, 'car','as14'],
    ['3', 'Jp nagar', 12.91660662, 77.52047465, 'second','as15']
];

/**
 * Function to init map
 */

function initialize() {
    var center = new google.maps.LatLng(12.9667,77.5667);
    var mapOptions = {
        zoom: 12,
        center: center,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    for (i = 0; i < markers1.length; i++) {
        addMarker(markers1[i]);
    }
}

/**
 * Function to add marker to map
 */

function addMarker(marker) {
    var category = marker[4];
    var title = marker[1];
    var pos = new google.maps.LatLng(marker[2], marker[3]);
    var content = marker[1];

    marker1 = new google.maps.Marker({
        title: title,
        position: pos,
        category: category,
        map: map
    });

    gmarkers1.push(marker1);

    // Marker click listener
    google.maps.event.addListener(marker1, 'click', (function (marker1, i) {
        return function () {
            console.log('Gmarker 1 gets pushed');
 var compiled = $compile('<div><div>' +marker1[i][0] + ' </div><div>' + marker1[i][1] + ' </div><div>' +marker1[i][2] + ' </div><div><button ng-click="getid(marker1[' + i + '][5])">Get</button></div></div>')($scope);

                                var infowindow = new google.maps.InfoWindow({
                                    content: compiled[0]
                                });
            //infowindow.setContent(content);
            infowindow.open(map, marker1);
            map.panTo(this.getPosition());
            map.setZoom(15);
        }
    })(marker1, i));
}
$scope.getid = function(id) {
                    console.log(id)

                    }
/**
 * Function to filter markers by category
 */

filterMarkers = function (category) {
    for (i = 0; i < markers1.length; i++) {
        marker = gmarkers1[i];
        // If is same category or category not picked
        if (marker.category == category || category.length === 0) {
            marker.setVisible(true);
        }
        // Categories don't match 
        else {
            marker.setVisible(false);
        }
    }
}

// Init map
initialize();
}//]]> 

</script>

</head>
<body>
  <div id="map-canvas"></div>
<select id="type" onchange="filterMarkers(this.value);">
    <option value="">Please select category</option>
    <option value="second">second</option>
    <option value="car">car</option>
    <option value="third">third</option>
</select>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

marker1是代码中的对象

marker1 = new google.maps.Marker({
    title: title,
    position: pos,
    category: category,
    map: map
});

然后你尝试访问其中的一些多维数组元素

var compiled = $compile('<div><div>' +marker1[i][0] + ' </div><div>' + marker1[i][1] + ...

所以修复这个错误,我认为它应该可行

但是为循环计数器

创建全局变量i是一种不好的做法