Google Maps StreetView InfowIndows opening on map

孤者浪人 提交于 2019-12-13 15:55:44

问题


When I add a marker and InfoWindow to my Google Maps application the marker is correctly added to both the map and the default StreetView panorama.

I also add InfoWindows to both copies of the marker by calling my bindInfoWindow function once with the map as an argument and once with the StreetView panorama as an argument.

This was working perfectly until a few weeks ago.

Now, for some reason, both InfoWindows are being displayed on the map, attached to the map marker.

I have created a simple fiddle (based on this public fiddle) which shows the problem here.

Basically I create the marker and InfoWindows in the normal way:

    var myMarker = new google.maps.Marker({
            map: map,
                position: new google.maps.LatLng(-34.397, 150.644),
                title: "My Marker",
                draggable:true,
            });

Then I use my bindInfoWindow function

function bindInfoWindow(marker, mapOrStreetView, whichInfoWindow, html, openWindow,markerId) {
    openWindow = ((typeof openWindow === 'undefined')?false:openWindow);
    markerId = ((typeof markerId === 'undefined')?'':markerId);
    google.maps.event.addListener(marker, 'click', function() {
        whichInfoWindow.setContent(html);
        whichInfoWindow.open(mapOrStreetView, marker);
    });
    if ( openWindow === true ) {
        whichInfoWindow.setContent(html);
        whichInfoWindow.open(mapOrStreetView, marker);
    }
}

to create the onClick actions for the markers and also open the InfoWindows automatically if required.

            var myMarkerInfoWindow = new google.maps.InfoWindow;
            bindInfoWindow(myMarker, map, myMarkerInfoWindow, "<h1>My Map Info Window Text<br /> <br /></h1>", true);
            var myMarkerStreetViewInfoWindow = new google.maps.InfoWindow;
            bindInfoWindow(myMarker, defaultStreetViewPanorama, myMarkerStreetViewInfoWindow, "<h1>My StreetView Info Window Text</h1>", true);

Up until about a week or two ago this was working perfectly, but all of a sudden the InfoWindows are both appearing on the map, rather than one appearing on the map and one appearing on the default StreetView.

In the fiddle, you can clearly see that the StreetView InfoWindow has opened over the top of the main map InfoWindow despite having been specified to open on the StreetView.

Note that map is my map object and defaultStreetViewPanorama is the StreetView Panormama object retrieved with

var defaultStreetViewPanorama = map.getStreetView();

just after the map object is created and just before the StreetView options are set. Note that the StreetView options are being correctly set, so it seems that map.getStreetView() is returning the correct object.


回答1:


Google broke this functionality in v3.25.

v3.24 was the last version which worked correctly.

v3.24 has been retired and is no longer available - there does not seem to be any simple workaround in the meantime.

The issue has been logged with Google on their bug-tracker at https://code.google.com/p/gmaps-api-issues/issues/detail?id=9925 and has been accepted but at the time of writing has not been fixed.

I will update this answer if/when there is any progress.




回答2:


There's a duplicate question here: InfoWindows on Markers in StreetView

This issue occurs when you are trying to use the same marker instance in a Google Map and in the StreetViewPanorama for that map and want to display infoWindows in both.

If you specify the marker in the open method of the infowindow for the StreetViewPanorama object, the infowindow that is supposed to display in StreetView, displays in the map. This is incorrect behavior.

My solution follows:

I've created a codepen that shows this fix: https://codepen.io/moutono/pen/KjZpZB

The problem here seems to be that the map object that is attached to the marker replaces the map object in the infoWindow when you open the infoWindow like this:

streetViewInfowindow.open(panorama, marker); //Won't work because the panorama object is replaced by the map object attached to the marker.

So if you want to display infoWindows on the same marker in both Street View and Map then you have to do it like this:

mapInfowindow.setPosition(position);
mapInfowindow.open(map);
streetViewInfowindow.setPosition(position);
streetViewInfowindow.open(panorama); //This works because panorama object is not replaced by map object from marker.

Link to Google Issue Tracker



来源:https://stackoverflow.com/questions/37778271/google-maps-streetview-infowindows-opening-on-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!