InfoWindows on Markers in StreetView

浪子不回头ぞ 提交于 2019-12-06 14:12:23
Fat Monk

My first error was that I was trying to display the same InfoWindow on both the map copy and the StreetView copy of the marker.

This is not allowed, and so creating a second instance of the InfoWindow fixed that problem.

In order to create two separate InfoWindows and attach them to the two copies of the same marker I had to modify my code somewhat.

The code above was from a function called bindInfoWindow() based on the code in the Google documentation. I modified this to include a parameter to specify either the map or the streetView object.

The next problem was that the clearListeners method was being called every time bindInfoWindow() was being called, effectively removing the onClick binding for the map copy of the marker.

I therefore moved the clearListeners call to outside of the function and call it before calling binInfoWindow().

The final function looks like below:

function bindInfoWindow(marker, mapOrStreetViewObject, infoWindowObject, html) {
    google.maps.event.addListener(marker, 'click', function() {
        infoWindowObject.setContent(html);
        infoWindowObject.open(mapOrStreetViewObject, marker);
    });
}

This is then called with the sequence:

// Note that the mapObject and streetViewObject variables are defined elsewheer to point to the map nd streetView instances in use.

//Define the local variables that we'll use in the calls
var myMapInfoWindow = new google.maps.InfoWindow;
var mapInfoWindowHTML = 'some stuff';
var myStreetViewInfoWindow = new google.maps.InfoWindow;
var streetViewInfoWindowHTML = 'some stuff';

// Remove any existing listeners from the marker
google.maps.event.clearListeners(marker,'click');

// Bind the event for the map marker click
bindInfoWindow(markerObject, mapObject, myMapInfoWindow, mapInfoWindowHTML);
//Bind the event for the StreetView marker click
bindInfoWindow(markerObject, streetViewObjectObject, myStreetViewInfoWindow, streetViewInfoWindowHTML);

What is nice about this is that if you open an InfoWindow on the map, then open StreetView the same InfoWindow is already open on the Street View!

TL;DR

I was able to make it work with the following steps:

  1. Have two (2) independent InfoWindows; one for the map, and one for the street view panorama
  2. Explicitly define the value for InfoWindowOptions.position when instantiating the InfoWindows
  3. Pass null as the argument to the anchor parameter of the InfoWindow.open() method

Example

// The geolocation point
var point = new google.maps.LatLng(10.5884708621,122.7016563883);

// The map
var map = new google.maps.Map(document.getElementById('map'), {
    center: point,
    zoom: 20,
    mapTypeId: "satellite",
});

// The marker
var marker = new google.maps.Marker({
    title: "Hello world!",
    position: point,
    label: {text:"hello",color:"#ffffffde",fontWeight:"bold",fontSize:"18px"},
    map: map
});

// The InfoWindow for the map view
var mapInfoWindow = new google.maps.InfoWindow({
    content: "foo",
    position: point // refer to step #2
});

// The InfoWindow for the street view
var streetViewPanoramaInfoWindow = new google.maps.InfoWindow({
    content: "bar",
    position: point, // refer to step #2
    disableAutoPan: true // optional but helpful
});

// The click event handler for the marker
marker.addListener('click', function() {
    var streetViewPanorama = map.getStreetView();

    // when streetview was engaged
    if(streetViewPanorama.getVisible()==true) {
        streetViewPanoramaInfoWindow.open(streetViewPanorama); // refer to step #3
    }
    // when normal aerial view was engaged
    else {
        mapInfoWindow.open(map); // refer to step #3
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!