javascript Google Maps API v3:InfoWindow 位置不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6617163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:17:53  来源:igfitidea点击:

Google Maps API v3: InfoWindow is not in right position

javascriptgoogle-mapsgoogle-maps-api-3

提问by Pow

I want the infoWindow to open on the specific marker every time I click on the marker. But every time it stays in the same position where it used to be at the last time. Here's my code

我希望每次单击标记时信息窗口都在特定标记上打开。但每次它都停留在上次以前的位置。这是我的代码

var infowindow = new google.maps.InfoWindow(
    { 
        size: new google.maps.Size(150,20)
    });
    var markersArray = [];
    var countMarker = 0;


    function initialize() {       
        geocoder = new google.maps.Geocoder();
        var myLocation = new google.maps.LatLng(52.13206069538749, -106.63635849952698);
        var mapOptions = {
            zoom: 12,
            center: myLocation,
            mapTypeControl: true,
            navigationControl: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map =  new google.maps.Map(document.getElementById("mapCanvas"), mapOptions); 
        google.maps.event.addListener(map, 'click', function(event) {

             addMarker(event.latLng);
             infowindow.setPosition(marker.getPosition());
             infowindow.setContent(event.latLng.toString());

             infowindow.open(map,marker);

             // showing latitude and longitude in infowindow


             if (flag == 1){
                document.getElementById("latbox").value=event.latLng.lat();  
                document.getElementById("lngbox").value=event.latLng.lng();
                flag++;
             }
             else{
                var current_lat = event.latLng.lat().toString();
                var current_lng = event.latLng.lng().toString();
                insRow(current_lat, current_lng, marker.id);
             }
             google.maps.event.addListener(marker, 'click', function() {
                // addMarker(event.latLng);
               // infowindow.setPosition(event.latLng);
               infowindow.setPosition(marker.getPosition());
                infowindow.setContent(event.latLng.toString());
                //infowindow.open(map,event.latLng);
             });
        });
        //document.write("3");
        google.maps.event.trigger(marker, 'click');       
    }    
function addMarker(location) {
        countMarker++;
        marker = new google.maps.Marker({
            position: location,
            map: map,
            id: countMarker
        });
        markersArray.push(marker);            
        //alert(marker.id); 
    }

Can anyone help me in this regard please?

任何人都可以在这方面帮助我吗?

回答by Bryan Weaver

try this:

试试这个:

            google.maps.event.addListener(marker, "click", function (event) {
                infoWindow.setContent(this.position);
                infoWindow.open(map, this);
               });

Also, I do not see in the code where your are defining the variable marker.

另外,我在代码中没有看到您定义变量标记的位置。

You might want to add change addMarker(event.latLng);to

您可能希望将更改添加addMarker(event.latLng);

var marker = addMarker(event.latLng); 

and change the function to return the marker object:

并更改函数以返回标记对象:

function addMarker(location) { 
   countMarker++;
   var marker = new google.maps.Marker({
                position: location,                    
                map: map,                     
                id: countMarker                
        }); 

   markersArray.push(marker);                           
   return marker;
}