javascript - 具有信息窗口行为的信息框

标签 javascript html google-maps-api-3

我一直致力于为我的一个网页实现 InfoBox 代码,并且通过大量的辅导我已经能够做到这一点。就目前而言,我可以在加载表单时让框显示在每个标记上,但我真的很希望它有它,以便在加载表单时不显示任何框,然后,当用户单击“标记 A',信息框出现并且 map 平移以使标记点击 map 的中心,就像信息窗口一样。

然后,如果用户单击“标记 B”,原始信息框将移动到新单击的标记,再次平移 map 并将 map 居中到该标记。

我查看了 Google 站点上的所有文档,但找不到如何执行此操作。我只是想知道是否有人可以向我展示热度来做这件事。请在下面找到我的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>All Locations</title>
        <link rel="stylesheet" href="css/alllocationsstyle.css" type="text/css" media="all" />
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
        <script type="text/javascript" src="js/infobox.js"></script>
        <script type="text/javascript"> 
            var customIcons = {
            0: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            },
            1: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            }
            };

            function load() { 
            var map = new google.maps.Map(document.getElementById("map"), { 
            center: new google.maps.LatLng(54.312195845815246,-4.45948481875007), 
            zoom:6, 
            mapTypeId: 'roadmap' 
            }); 

            // Change this depending on the name of your PHP file 
            downloadUrl("phpfile.php", function(data) { 
            var xml = data.responseXML; 
            var markers = xml.documentElement.getElementsByTagName("marker");
            var bounds = new google.maps.LatLngBounds();
            for (var i = 0; i < markers.length; i++) { 
            var locationname = markers[i].getAttribute("locationname"); 
            var address = markers[i].getAttribute("address");
            var totalfinds = markers[i].getAttribute("totalfinds");
            var point = new google.maps.LatLng( 
            parseFloat(markers[i].getAttribute("osgb36lat")), 
            parseFloat(markers[i].getAttribute("osgb36lon")));
            var html = locationname + "<p>" + 'No. of finds: ' + "<b>" + totalfinds + "</b>" + "</p>";
            var icon = {}; 
            if (totalfinds == 0) {   
            icon = customIcons[0]; 
            } else if (totalfinds >= 1) {   
            icon = customIcons[1];      
            } 
            var marker = new google.maps.Marker({          
            map: map, 
            position: point,
            title: address,
            icon: icon.icon,
            shadow: icon.shadow
            }); 

            var boxText = document.createElement("div");

            boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";        
            boxText.innerHTML =  locationname + "<p>" + 'No. of finds: ' + "<b>" + totalfinds + "</b>" + "</p>";

            var myOptions = {
            content: boxText
            ,disableAutoPan: false
            ,maxWidth: 0
            ,pixelOffset: new google.maps.Size(-140, 0)
            ,zIndex: null
            ,boxStyle: { 
            background: "url('tipbox.gif') no-repeat"
            ,opacity: 0.75
            ,width: "280px"
            }
            ,closeBoxMargin: "10px 2px 2px 2px"
            ,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
            ,infoBoxClearance: new google.maps.Size(1, 1)
            ,isHidden: false
            ,pane: "floatPane"
            ,enableEventPropagation: false
            };

开始编辑

    bounds.extend(point);
    map.fitBounds(bounds);
    var infoBox = new InfoBox(myOptions);
    google.maps.event.addListener(marker, 'click', function() {
    infoBox.open(map, this); 
    map.setCenter(this.position;
    });
    } 
    }); 
    } 

编辑结束

            function downloadUrl(url, callback) { 
            var request = window.ActiveXObject ? 
            new ActiveXObject('Microsoft.XMLHTTP') : 
            new XMLHttpRequest; 


            request.onreadystatechange = function() { 
            if (request.readyState == 4) { 
            request.onreadystatechange = doNothing; 
            callback(request, request.status); 
            } 
            }; 

            request.open('GET', url, true); 
            request.send(null); 
            } 

            function doNothing() {} 

            </script> 
            </head>    
            <body onLoad="load()">
                <div id="map"></div>
            </body> 
            </html>

最佳答案

我从未使用过 infoBox,但我认为它与标准信息窗口非常相似。您需要使用 Events将点击操作添加到将打开信息框并使 map 居中的标记。

类似的东西:

//register the single left click event.
google.maps.event.addListener(marker, "click", function (event) {

    //you may have to create a another box here or just change the content
    //Google's standard info window only creates one instance and then you change
    //the content during this event i.e. infoWindow.setContent("some html");

    infoBox.open(map, this);
    map.setCenter(this.position);
});

更新的答案:

好的,看起来您在循环的每次迭代中都覆盖了 infoBox,因此唯一要显示的将是最后一个创建的。您可以在循环外构建一个信息框,但不要设置内容属性,然后在监听器内部设置内容。

您需要将内容存储在其他地方以防止它也被覆盖。 尝试在标记中设置内容并像这样引用它

var boxText = document.createElement("div");
    boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";        
    boxText.innerHTML =  locationname + "<p>" + 'No. of finds: ' + "<b>" + totalfinds + "</b>" + "</p>";

var marker = new google.maps.Marker({          
            map: map, 
            position: point,
            title: address,
            icon: icon.icon,
            shadow: icon.shadow,
            html: boxText 
            });

google.maps.event.addListener(marker, "click", function (event) {
    infoBox.setContent(this.html);
    infoBox.open(map, this);
    map.setCenter(this.position);
});

关于javascript - 具有信息窗口行为的信息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7338349/

相关文章:

javascript ajax 缓存后不起作用

html - 为什么IE7没有背景图片?

javascript - 滚动时将 div 粘在屏幕顶部

google-maps - "Hole"居中的 Google Maps API 多边形

javascript - 将元素放入卡中的问题

javascript - OpenLayers map 未正确加载

javascript - __non_webpack_require__ 未定义

javascript - 无法让 jquery 处理 php 文件

javascript - 如果 infoWindowContent 中的字段为空,则 Google map 标记不会显示

javascript - 在 Unity 3D 上使用 Google map API v3