为每个标记设置Google地图的内容

时间:2014-11-21 19:11:12

标签: javascript json google-maps google-maps-api-3

需要快速帮助设置我正在创建的每个标记的内容

我正在遍历我的Locations Obj,它为每个标记(locationObj)保存Lat,Lng, 每个标记的内容由其他obejct(PermutationObj)保存。

示例:

 locationObj-- > {"Lat":"34.163291","Lng":"-118.685123"} etc....

 PermutationObj -- > ElectronicPopContent,LocationName 

问题是,我正在为地图上显示的所有标记获取PermutationObj中的第一个内容和位置名称。

我该如何解决?

JS代码:

var locationObj;
        var PermutationObj;
        var map;
        var mapOptions;

        var shape = {
            coord: [1, 1, 1, 20, 18, 20, 18, 1],
            type: 'poly'
        };





      function setMap(locationList, permutation) {
            // List of Lat,Lng
            locationObj = $.parseJSON(locationList);
            PermutationObj = $.parseJSON(permutation);


            var qMapTypeId = google.maps.MapTypeId.SATELLITE
            var LatLngCenter = new google.maps.LatLng(34.163291, -118.685123);
            var mapOptions = {
                zoom: 4,
                center: LatLngCenter,
                mapTypeControl: true,
                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                }
            }
            var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

            var bounds = new google.maps.LatLngBounds();
            for (i in locationObj) {
                var LatLngPair = new google.maps.LatLng(locationObj[i]["Lat"], locationObj[i]["Lng"]);

                bounds.extend(LatLngPair);
                map.fitBounds(bounds);


//                for (j in PermutationObj) {
//                    //  var image = PropObj[j]["ElectroincImageURL"];
//                    var content = PermutationObj[j]["ElectronicPopContent"];
//                    break
//                }

                var content = PermutationObj[i]["ElectronicPopContent"];

                marker = new google.maps.Marker({
                    position: LatLngPair,
                    map: map,
                    draggable: false,
                    //                    icon: image,
                    shape: shape,
                    anchorPoint: new google.maps.Point(12, -25)

                });

                //Setting up the content of the info window dynamic wise.
                var infowindow = new google.maps.InfoWindow({
                    content: content
                });



                //Setting up an event listener, When the user clicks the marker, an info window opens.
                google.maps.event.addListener(marker, 'click', function () {
                    infowindow.setContent(content);
                    infowindow.open(map, marker);
                });
            }
        }

1 个答案:

答案 0 :(得分:0)

更改了一些内容,第一个是只有一个InfoWindow实例,另一个是将内容存储为标记属性并在点击时使用它,最后一个是使用{{1}每个标记实例而不是var marker = ...让我知道这是否适合你。

marker = ...