Google 地图 v3 居中和 Infowindow 控制

发布于 2025-01-03 19:33:03 字数 3404 浏览 1 评论 0原文

我有一张地图,其中有复选框供用户选择他们想要在地图上显示的内容。除了以下两项之外,一切都运行良好。

  1. 当用户从列表中选择一个项目时,标记确实会显示在地图上,但地图不会重新以标记为中心。当页面加载时,对于位于地图可视区域之外的标记来说,这是必需的。

  2. 当我在地图上打开多个标记时,我希望一次只打开 1 个信息窗口,目前如果我单击 10 个标记,就会打开 10 个信息窗口。我希望如果打开一个信息窗口并单击另一个标记,则第一个信息窗口关闭。

我已经粘贴了下面一个标记的代码片段,任何帮助将不胜感激!

    jQuery(document).ready(function(){
        /**
        * The Map object.
        * @type {google.maps.Map}
        */
        var mapOptions = {
            center: new google.maps.LatLng(36.812946,-119.746953),
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.SATELLITE
        };
        var map = new google.maps.Map(document.getElementById("map"), mapOptions);



        /**
        * The markers array.
        * @type {Object}
        */
        var markers = {};


            markers.building37 = [];

            var marker0237  = new google.maps.Marker({
                visible: false,
                icon: new google.maps.MarkerImage("images/website/brown_Marker.png",new google.maps.Size(32,37),null,null),
                title: 'Building',
                position: new google.maps.LatLng(36.80694607313768,-119.73590791225433),
                center: position,
                map: map
            });

            markers.building37.push(marker0237);  

            var info_window0237 = new google.maps.InfoWindow({
                content: '<div id="infobubble"><div id="img"><img src="images/buildings/Foundation001.jpg" alt="Foundation Building"></div><div id="desc"><h3>Foundation</h3></div></div>',
                maxWidth:350,
            });

            google.maps.event.addListener(marker0237, "click", function() {
                info_window0237.open(map,marker0237);
            });



    var showBuilding37 = false;

    var mgrBuilding37 = null;


        /**
        * Toggles Building 37 Marker Group visibility.
        */
        function toggleBuilding37()
        {
            showBuilding37 = !showBuilding37;
            if (showBuilding37)
                for (var i=0; i < markers.building37.length; i++)
                    markers.building37[i].setVisible(true);
            if (mgrBuilding37)
            {
                if (showBuilding37)
                {
                    mgrBuilding37.addMarkers(markers.building37, 0);
                    mgrBuilding37.refresh();
                } 
                else
                {
                    mgrBuilding37.clearMarkers();
                    mgrBuilding37.refresh();
                }
            }
            else 
            {
                mgrBuilding37 = new MarkerManager(map, {trackMarkers: true, maxZoom: 15});
                google.maps.event.addListener(mgrBuilding37, "loaded", function() {
                    if (showBuilding37)
                    {
                        mgrBuilding37.addMarkers(markers.building37, 0);
                        mgrBuilding37.refresh();
                    } 
                    else
                    {
                        mgrBuilding37.clearMarkers();
                        mgrBuilding37.refresh();
                    }
                });
            }
        }            

          google.maps.event.addDomListener(
              document.getElementById("building37-cb"),"click", toggleBuilding37);  

    });

I have a map that has checkboxes for users to select what they would like displayed on the map. Everything works well with the exception of the following two items.

  1. When a user selects an item from the list the marker does display on the map, however the map does not re-center on the marker. This is needed for a marker that is outside the viewable area of the map when the page loads.

  2. When I have multiple markers open on the map I would like to have only 1 infowindow open at a time, currently if I click on 10 markers there will be 10 infowindows open. I would like to have it where if an infowindow is open and another marker is clicked then the first infowindow closes.

I have pasted a snippet of the code for one of the markers below, any help would be greatly appreciated!

    jQuery(document).ready(function(){
        /**
        * The Map object.
        * @type {google.maps.Map}
        */
        var mapOptions = {
            center: new google.maps.LatLng(36.812946,-119.746953),
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.SATELLITE
        };
        var map = new google.maps.Map(document.getElementById("map"), mapOptions);



        /**
        * The markers array.
        * @type {Object}
        */
        var markers = {};


            markers.building37 = [];

            var marker0237  = new google.maps.Marker({
                visible: false,
                icon: new google.maps.MarkerImage("images/website/brown_Marker.png",new google.maps.Size(32,37),null,null),
                title: 'Building',
                position: new google.maps.LatLng(36.80694607313768,-119.73590791225433),
                center: position,
                map: map
            });

            markers.building37.push(marker0237);  

            var info_window0237 = new google.maps.InfoWindow({
                content: '<div id="infobubble"><div id="img"><img src="images/buildings/Foundation001.jpg" alt="Foundation Building"></div><div id="desc"><h3>Foundation</h3></div></div>',
                maxWidth:350,
            });

            google.maps.event.addListener(marker0237, "click", function() {
                info_window0237.open(map,marker0237);
            });



    var showBuilding37 = false;

    var mgrBuilding37 = null;


        /**
        * Toggles Building 37 Marker Group visibility.
        */
        function toggleBuilding37()
        {
            showBuilding37 = !showBuilding37;
            if (showBuilding37)
                for (var i=0; i < markers.building37.length; i++)
                    markers.building37[i].setVisible(true);
            if (mgrBuilding37)
            {
                if (showBuilding37)
                {
                    mgrBuilding37.addMarkers(markers.building37, 0);
                    mgrBuilding37.refresh();
                } 
                else
                {
                    mgrBuilding37.clearMarkers();
                    mgrBuilding37.refresh();
                }
            }
            else 
            {
                mgrBuilding37 = new MarkerManager(map, {trackMarkers: true, maxZoom: 15});
                google.maps.event.addListener(mgrBuilding37, "loaded", function() {
                    if (showBuilding37)
                    {
                        mgrBuilding37.addMarkers(markers.building37, 0);
                        mgrBuilding37.refresh();
                    } 
                    else
                    {
                        mgrBuilding37.clearMarkers();
                        mgrBuilding37.refresh();
                    }
                });
            }
        }            

          google.maps.event.addDomListener(
              document.getElementById("building37-cb"),"click", toggleBuilding37);  

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走就滚别墨迹 2025-01-10 19:33:03

我不明白你在哪里设置地图中心。

  1. 添加标记时,您应该执行类似 map.setCenter(location); 的操作。

  2. 您应该保留一个信息窗口列表,当您显示一个信息窗口时,循环浏览其他窗口并隐藏它们的信息窗口。

     //声明你的信息窗口数组
        var infoWindows = new Array();
    
        var info_window0237 = 新的 google.maps.InfoWindow({
            content: '
    基础建筑

    基础

    ', 最大宽度:350, }); //创建信息窗口后,将其添加到数组中 infoWindows.push(info_window0237); google.maps.event.addListener(marker0237, "点击", function() { //当点击显示信息窗口时,隐藏其余部分 for(i = 0; i < indowWindows.length; i++) { //这将关闭您添加到数组中的所有信息窗口 infoWindows[i].close(); } info_window0237.open(地图,marker0237); });

I don't see where you are setting the map center.

  1. You should do something like map.setCenter(location); when you add a marker.

  2. You should keep a list of info windows and when you display one, loop through the others and hide their info windows.

        //Declare your info window array
        var infoWindows = new Array();
    
        var info_window0237 = new google.maps.InfoWindow({
            content: '<div id="infobubble"><div id="img"><img src="images/buildings/Foundation001.jpg" alt="Foundation Building"></div><div id="desc"><h3>Foundation</h3></div></div>',
            maxWidth:350,
        });
    
        //After you make an infowindow, add it to the array
        infoWindows.push(info_window0237);
    
        google.maps.event.addListener(marker0237, "click", function() {                          
              //When you show an infowindow on click, hide the rest
              for(i = 0; i < indowWindows.length; i++)
              {
                   //this will close all the infowindows you added to the array
                   infoWindows[i].close();
              }
              info_window0237.open(map,marker0237);
    
        });
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文