休息标记位置 + 信息窗口位置

Rest the marker position + info window position

本文关键字:位置 信息 信息窗 窗口      更新时间:2023-09-26

我在谷歌地图中设置信息窗口的位置时遇到问题。

我知道这听起来有点担心,但我的愿望是,如果纬度,lng 为 0,0,我想将标记放在美国地图的中心,并将信息窗口的位置停留在同一位置。

希望有人能帮助我...

  map = new google.maps.Map(document.getElementById("map-canvas"));
                //I'm getting from an object that I'm looping on it coordinates 0,0
                var myLatLng = new google.maps.LatLng(0, 0);

                // Create the marker on the map
                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map
                });
                // Check if the LatLng are 0,0  - hide the marker redefine the position of the marker + map position.
                if (myLatLng == "0,0") {
                    var CenterLatLng = new google.maps.LatLng(39.828127, -98.579404);
                    marker.setVisible(false);
                    marker.setPosition(CenterLatLng);
                    map.setCenter(new google.maps.LatLng(39.828127, -98.579404));
                }

                  var contentString = '<div id="content">'+
                  '<div id="siteNotice">'+
                  '</div>'+
                  '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
                  '<div id="bodyContent">'+
                  '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
                  'sandstone rock formation in the southern part of the '+
                  'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
                  'south west of the nearest large town, Alice Springs; 450&#160;km '+
                  '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
                  'features of the Uluru - Kata Tjuta National Park. Uluru is '+
                  'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
                  'Aboriginal people of the area. It has many springs, waterholes, '+
                  'rock caves and ancient paintings. Uluru is listed as a World '+
                  'Heritage Site.</p>'+
                  '<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
                  'https://en.wikipedia.org/w/index.php?title=Uluru</a> '+
                  '(last visited June 22, 2009).</p>'+
                  '</div>'+
                  '</div>';
                  var infowindow = new google.maps.InfoWindow({
                      content: contentString
                  });


                  google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map,marker); // When The info window is open its grabbing the correct value that I defined in the above but it's opening it somewhere else on the map
                  });

要检查 google.maps.LatLng 值,请使用 google.maps.LatLng 方法 .equals, not (myLatLng == "0,0")

工作小提琴