下拉菜单(边栏)

时间:2011-07-24 18:39:32

标签: html google-maps-api-3

我将以下代码放在一起,从我的SQL数据库加载标记数据,正确显示所有信息。

我现在想要包含一个下拉菜单,通过“位置名称”显示标记,当用户选择相关标记反弹的位置时。我从一些信息中得知,我发现这是一个侧边栏?或许有人可以指出我正确的方向,我将如何调整我的代码以包含这个。

<!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/style.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"> 

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"); 
for (var i = 0; i < markers.length; i++) { 
var name = markers[i].getAttribute("locationname"); 
var address = markers[i].getAttribute("address"); 
var point = new google.maps.LatLng( 
parseFloat(markers[i].getAttribute("osgb36lat")), 
parseFloat(markers[i].getAttribute("osgb36lon"))); 
var marker = new google.maps.Marker({ 
map: map, 
position: point, 
}); 
} 
}); 
} 

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>

更新

<!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>Map My Finds - 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>
        <a href="javascript:;" title="Locations" onclick="animate(this)">Location Name</a> 
        <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("mapmyfindsloadalllocations.php", function(data) { 
            var xml = data.responseXML; 
            var markers = xml.documentElement.getElementsByTagName("marker"); 
            for (var i = 0; i < markers.length; i++) { 
            var locationname = markers[i].getAttribute("locationname"); 
            var address = markers[i].getAttribute("address");
            var finds = markers[i].getAttribute("finds");
            var point = new google.maps.LatLng( 
            parseFloat(markers[i].getAttribute("osgb36lat")), 
            parseFloat(markers[i].getAttribute("osgb36lon")));
            var icon = customIcons[finds] || {};
            var marker = new google.maps.Marker({          
            map: map, 
            position: point,
            title: locationname + ' - ' + address,
            icon: icon.icon,
            shadow: icon.shadow
            }); 
            } 
            }); 
            } 
            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> 
            <script type="text/javascript"> 
            function animate(element) {    
            var marker = getMarkerByName(element.title);    
            marker.setAnimation(google.maps.Animation.BOUNCE);    
            setTimeout(function() { marker.setAnimation(null); }, 200); }  
            function getMarkerByName(name) {    
            // retrieve and return the marker from where you stored it } 
            </script> 
            </head> 
            <body onLoad="load()">
                <div id="map"></div>
                <label></label>
            </body> 
            </html>

1 个答案:

答案 0 :(得分:0)

这是一个想法:
将每个位置名称与下拉菜单中的链接相关联 存储每个位置的标记 当您从下拉菜单中单击链接时,检索与该位置关联的标记,然后只需调用一个为其设置动画的功能:

 ....
<!-- assuming this is an item from the menu -->
 <a href="javascript:;" title="locationName" onclick="animate(this)">Location Name </a>
 ....

剧本:

<script type="text/javascript">
function animate(element) {
   var marker = getMarkerByName(element.title);
   marker.setAnimation(google.maps.Animation.BOUNCE);
   setTimeout(function() { marker.setAnimation(null); }, 200);
}

function getMarkerByName(name) {
   // retrieve and return the marker from where you stored it
}
</script>
...

像这样的东西

<强>更新

基本没有变化......当您点击链接时,突出显示它并调用动画功能 要在标记点击时突出显示正确的链接,请单击“标记”每个标记:

google.maps.event.addListener(marker, 'click', function(event) {
     // animate marker
     // highlight the link associated with the marker.
});