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

时间:2015-01-23 19:29:01

标签: javascript google-maps google-maps-api-3

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

我知道它听起来有点担心,但我的愿望是,如果lat,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
                  });

1 个答案:

答案 0 :(得分:0)

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

working fiddle