简体   繁体   English

Google地图信息窗口不起作用?

[英]Google map info window is not working?

I have enclosed my code here i am struggling to pass values to info window i am getting Uncaught TypeError: Cannot read property '0' of undefined . 我在这里附上我的代码,我正在努力将值传递给信息窗口,但是我遇到了Uncaught TypeError:无法读取undefined的属性“ 0”。 Expectation: 1.I want to display values in infowindow 2.when i click button it should pass the id value i have added fiddle http://jsfiddle.net/cLADs/135/ 期望: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 is an object in your code marker1是代码中的对象

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

and then you try to access some multidimension array elements inside it 然后尝试访问其中的一些多维数组元素

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

So fix this bug and I think it should work 因此,请修复此错误,我认为它应该可以工作

But there is a bad practice to create a global variable i for the loop counter 但是,为循环计数器创建全局变量i是一种不好的做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM