【问题标题】:Google Map API - infowindow in foreach loopGoogle Map API - foreach 循环中的信息窗口
【发布时间】:2013-11-05 08:53:55
【问题描述】:

您好,我正在从 SqlServerCe 检索数据,因此我创建了 foreach 循环来创建标记 - 它可以创建多个标记,但现在我想为每个标记添加一个信息窗口。但是现在每当我点击标记时,信息窗口都会在最后创建的标记上弹出。

<script>
function initialize() {
    var mapProp = {
        center:new google.maps.LatLng(51.508742,-0.120850),
        zoom:5,
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };
    var map=new google.maps.Map(document.getElementById("googleMap")
      , mapProp);

    $(function () {
 @foreach (var row in data)
 {
   <text>
        var marker = new google.maps.Marker({ position: new google.maps.LatLng(@row.GeoLat,  @row.GeoLong),
            map: map });

        marker.info = new google.maps.InfoWindow({
            content: "test"
        });

        google.maps.event.addListener(marker, 'click', function() {
            marker.info.open(map, marker);
        });

        </text>


 }
    });
}



google.maps.event.addDomListener(window, 'load', initialize);

有人可以帮我为每个创建的标记添加信息窗口吗?

感谢您的回复和您的时间。

这就是我从 SqlServerCe 加载的方式

var db = Database.Open("StarterSite");

var data = db.Query("SELECT DescriptionService,GeoLong,GeoLat FROM services");
var array = new []{data} ;

【问题讨论】:

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


    【解决方案1】:

    您可以使用以下用javascript编写的

    var infoWindowContent = [];
    for(var index=0; index< places.length; index++){
            infoWindowContent[index] = getInfoWindowDetails(places[index]);
            var location = new google.maps.LatLng(places[index].latitude,places[index].longitude);
            bounds.extend(location);
            marker = new google.maps.Marker({
                position    : location,
                map         : map,
                title       : places[index].title
            });
    
            google.maps.event.addListener(marker, 'click', (function(marker,index){
                return function(){
                    infoWindow.setContent(infoWindowContent[index]);
                    infoWindow.open(map, marker);
                    map.setCenter(marker.getPosition());
                    map.setZoom(15);
                }
            })(marker,index));
    
        }   
    
    
        function getInfoWindowDetails(location){
            var contentString = '<div id="content" style="width:270px;height:100px">' +
                                '<h3 id="firstHeading" class="firstHeading">' + location.title + '</h3>'+
                                '<div id="bodyContent">'+
                                    '<div style="float:left;width:100%">'+ location.address + '</div>'+
                                '</div>'+
                            '</div>';
            return contentString;
        }
    

    我添加了一个数组infoWindowContent,然后将信息添加到数组中。你可以使用相同的逻辑

    【讨论】:

    • 感谢您的回答。我可以知道如何从 SqlServerCe 加载数据以像您一样使用它吗?请查看我的编辑。
    • 这里我创建了一个 JSON 数组的地方。然后我添加了该对象的内容。您可以创建一个 JSON 对象并在那里使用我的代码,也可以将代码更改为查询的返回值
    • 我想我明白这一点,但请我知道places 下隐藏了什么?它是一个数组,其中存储了来自 SqlServerCe 的数据?您能否与我分享将数据从 SqlServer 加载到数组的代码?非常感谢您。
    • @Marek:我没有使用 SqlServerCe。我在我的网络应用程序中使用 mysql。我正在根据从数据库接收到的数据创建一个 JSON 对象,这些位置只是一个对象数组,如下[{ title="place 1", address="my Location"}, { title="place 2", address="my Location 2 "}]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多