Невозможно отметить более одной координаты в Картах Google - PullRequest
0 голосов
/ 15 октября 2010

Ниже приведен код для установки маркеров в Картах Google.Но устанавливается только один маркер, тогда как четыре маркера должны быть установлены.

Что не так с этим кодом?

<!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>

1 Ответ

1 голос
/ 15 октября 2010

Мне удалось пометить несколько точек, изменив способ маркировки точек.Точки должны быть помечены перед добавлением на карту, по-видимому.Однако я не смог установить несколько информационных окон.Отображается только одно информационное окно, независимо от того, на каком маркере нажата кнопка.Обновленный код приведен ниже -

функция setMarkers (карта, местоположения) {for (var i = 0; i

                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());
        }
    }
...