代码之家  ›  专栏  ›  技术社区  ›  Jason Steve

HTML5地理位置信息窗口不工作

  •  -1
  • Jason Steve  · 技术社区  · 6 年前

    我需要用这段代码创建一个信息窗口,我缺乏应用程序逻辑,对我使用的编程完全陌生 open-source code

    此代码将用户及其标记添加到地图中

        function loadMarkers(data) {
        for(key in data) {
            var user = data[key];
            xmartlabschat.addUser(user);
            markers[key] = getMarker(user.lat, user.lng, user.name);
        }
    }
    

    我试图使用信息窗口的基本代码

    var infoWindow = new google.maps.InfoWindow(user.name);
    markers[key].addListener('click', function(){
            infoWindowMe.open(map,markers[key);
    

    但是,由于im单击了其他用户标记,该代码将所有标记转换为与用户相同的名称,而不是不同的名称

    请帮忙 Here the screenshot I took

    1 回复  |  直到 6 年前
        1
  •  1
  •   Foysal Nibir    6 年前

    你可以试试这个。希望这有帮助。

    var locations = [
    ['Location name', LAT, LON],
    ['...', ..., ...],
    //...
    ];
    
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 8,
        center: new google.maps.LatLng(LAT, LON),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var infowindow = new google.maps.InfoWindow();
    
    var marker, i; 
    for (i = 0; i < locations.length; i++) {
        var lat = locations[i][1];
        var lng = locations[i][2];
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng),
            map: map,
        }); // generates all the markers
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                zIndex: 1000;
                infowindow.setContent(locations[i][0]); // replace locations[i][0] with which data you want to show
                infowindow.open(map, marker);
            }
        })(marker, i)); // generates corresponding info-window
    }