简体   繁体   English

Google Maps API JS V3:infowindow.getPosition()==未定义?

[英]Google Maps API JS V3: infowindow.getPosition() == undefined?

I wanted to make something to toogle if the infowindow is visable. 如果可以看到信息窗口,我想做些麻烦的事情。 My expression is: 我的表达是:

    if(infowindow.getPosition() != marker.getPosition())

The porblem is, that infowindow.getPosition() returns undefined, also if it has a position. 问题是,infowindow.getPosition()返回未定义的值,即使它有位置也是如此。
JSFiddle-Demo 的jsfiddle-演示

What did I do wrong? 我做错了什么?

Full Code: 完整代码:

<html>
        <head>
                <style type="text/css">
                    html,body,#map-canvas {
                        height: 100%;
                        width:100%;
                        margin: 0;
                        padding: 0;
                    }
                </style>
                <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
                <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?language=de&v=3"></script>
                <script type="text/javascript">

                    $( document ).ready(function() {
                        var map = new google.maps.Map(document.getElementById("map-canvas"), {
                            center: new google.maps.LatLng(50.2881829, 11.4835767),
                            zoom: 6
                        });

                        var marker = new google.maps.Marker({
                            position: new google.maps.LatLng(50.2881829, 11.4835767),
                            map: map,
                        });

                        var infowindow = new google.maps.InfoWindow({
                            content: '<div id="content">test123</div>'
                        });

                        google.maps.event.addListener(marker, 'click', function() {
                            console.log(infowindow.getPosition());
                            console.log(marker.getPosition());
                            if(infowindow.getPosition() != marker.getPosition()) {
                                infowindow.open(map,marker);
                            } else {
                                infowindow.close();
                            }
                        });
                    });
                </script>
        </head>
        <body>
            <div id="map-canvas"></div>
        </body>
    </html>

As it seems the open -method doesn't update the position of the infowindow , you'll need to do it on your own(eg by binding the position of the infowindow to the position of the marker): 因为它似乎在open -方法不更新position的的infowindow ,你需要做你自己的(例如,通过结合信息窗口的到标记的位置的位置):

    infowindow.unbind('position');
    if(infowindow.getPosition() != this.getPosition()) {
        infowindow.bindTo('position',this,'position');
        infowindow.open(map,this);
    } else {           
        infowindow.close();
        infowindow.setPosition(null);
    }

Demo: http://jsfiddle.net/aBg3N/ 演示: http//jsfiddle.net/aBg3N/

Another solution(but this solution relies on a undocumented property anchor ): 另一个解决方案(但此解决方案依赖于未记录的属性anchor ):

    if(infowindow.get('anchor') != this) {
        infowindow.open(map,this);
    } else {
        infowindow.close();
    }

Demo: http://jsfiddle.net/AZC3z/ 演示: http//jsfiddle.net/AZC3z/

Both solutions will work with draggable markers and also when you use the same infowindow for multiple markers 两种解决方案都可与可拖动标记一起使用,并且当您对多个标记使用相同的信息窗口时,

I am not sure about infowindow.getPosition() . 我不确定infowindow.getPosition() But you can try this code if you want to check whether the infowindow is open or not. 但是,如果要检查信息窗口是否打开,可以尝试使用此代码。

JS: JS:

function check(infoWindow) {
    var map = infoWindow.getMap();
    return (map !== null && typeof map !== "undefined");
}

pass infowindow into the function and it will return true or false based on visibility of infowindow . infowindow传递到函数中,它将基于infowindow可见性返回truefalse

Demo: http://jsfiddle.net/lotusgodkk/x8dSP/3699/ 演示: http : //jsfiddle.net/lotusgodkk/x8dSP/3699/

Apparently the google.maps.InfoWindow won't have a position unless you set one, if I do this: 显然,除非您设置一个位置,否则google.maps.InfoWindow不会有位置,如果这样做的话:

var infowindow = new google.maps.InfoWindow({
    content: '<div id="content">test</div>',
    position: new google.maps.LatLng(0,0)
});

fiddle 小提琴

or: 要么:

var infowindow = new google.maps.InfoWindow({
    content: '<div id="content">test</div>',
    position: marker.getPosition()
});

infowindow.getPosition() returns the value set. infowindow.getPosition()返回设置的值。

Unfortunately, that can't be used to detect whether or not the infowindow is currently visible or not, so your test won't work as expected. 不幸的是,这不能用来检测当前是否可见信息窗口,因此您的测试将无法按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM