English 中文(简体)
无法在谷歌地图中标记多个坐标
原标题:Unable to mark more than one coordinate in Google Maps

下面是列出的在谷歌地图中设置标记的代码。但是,只设置了一个标记,而应该设置四个标记。

这个代码出了什么问题?

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript"
    src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript"> 
var locations = [
[4, -33.890542, 151.274856, Town1 ,  Page1.aspx ],
[3, -33.923036, 151.259052, Town2 ,  Page1.aspx ],
[2, -34.028249, 151.157507, Town3 ,  Page1.aspx ],
[1, -33.80010128657071, 151.28747820854187, Town4 ,  Page1.aspx ]
];
</script>

<script type="text/javascript">
    function initialize() {
            var myOptions = {
                zoom: 10,
                center: new google.maps.LatLng(-33.9, 151.2),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            var map = new google.maps.Map(document.getElementById("map_canvas"),
                                myOptions);

            setMarkers(map, locations);
        }

        /**
        * Data for the markers consisting of a name, a LatLng and a zIndex for
        * the order in which these markers should display on top of each
        * other.
        */


        function setMarkers(map, locations) {
            debugger;
            for (var i = 0; i < locations.length; i++) {
                var location = locations[i];
                if (location != undefined) {
                    var myLatLng = new google.maps.LatLng(location[1], location[2]);
                    var marker = new google.maps.Marker({
                        position: myLatLng,
                        map: map,
                        title: location[3],
                        zIndex: location[0]
                    });
                }

                var latlngbounds = new google.maps.LatLngBounds();
                for (var i = 0; i < locations.length; i++) {
                    var location = locations[i];
                    if (location != undefined) {
                        var point = new google.maps.LatLng(location[1], location[2]);
                        latlngbounds.extend(point);
                    }
                }
                map.setCenter(latlngbounds.getCenter());
            }
        }


</script>
</head>
<body onload="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
最佳回答

我能够通过改变点的标记方式来标记多个点。在明显地添加到地图之前,应该标记点。但是,我无法设置多个信息窗口。无论单击哪个标记,都只显示一个信息窗口。预计的代码如下所示-

function setMarkers(map, locations) { for (var i = 0; i < locations.length; i++) { var location = locations[i]; if (location != undefined) { var latlngbounds = new google.maps.LatLngBounds(); var point = new google.maps.LatLng(location[1], location[2]);

                latlngbounds.extend(point);

                var myLatLng = new google.maps.LatLng(location[1], location[2]);
                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    title: location[3],
                    zIndex: location[0]
                });
                var content = "<a href= " + location[4] + " ><strong>" + location[3] + "</strong></a>";
                var infowindow = new google.maps.InfoWindow({
                    content: content
                });

                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    title: location[3]
                });
                google.maps.event.addListener(marker,  click , function () {
                    infowindow.open(map, marker);
                });
            }

            map.setCenter(latlngbounds.getCenter());
        }
    }
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签