我在这里附上我的代码,我正在努力将值传递给信息窗口,但是我遇到了Uncaught TypeError:无法读取undefined的属性“ 0”。
期望:
1.我想在信息窗口中显示值
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>

最佳答案

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是一种不好的做法

关于javascript - Google map 信息窗口不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32657551/

10-10 22:34