Google Maps infoWindow without marker?

后端 未结 3 1431
名媛妹妹
名媛妹妹 2021-01-05 23:46

According to the documention a marker is optional with an infoWindow, so how is this achieved please? I have tried infowindow.open(map) and infowindow.open(map,

相关标签:
3条回答
  • 2021-01-06 00:02

    infowindow.open(map) makes no sense since you need to specify the position where the infowindow should open, also it has a tapered stem which specifies the location where it should point.

    According to the documentation of Infowindows- InfoWindows may be attached to either Marker objects (in which case their position is based on the marker's location) or on the map itself at a specified LatLng.

    So if you donot want the marker to open the infowindow, use the map click event-

    var iwindow= new google.maps.InfoWindow;
        google.maps.event.addListener(map,'click',function(event)
        {
             iwindow.setContent(event.latLng.lat()+","+event.latLng.lng());
             iwindow.open(map,this); 
             iwindow.open(map,this);
        });
    

    And to close a InfowWindow- infowindow.setMap(null);

    Hope that cleared your doubts.

    0 讨论(0)
  • 2021-01-06 00:10

    Since the api has changed, it is not possible to set position on info window any more. the solution i found is to add a marker to the map with visibility false:

    this.map.addMarker({
            position: {
              'lat': sites[0].latitude,
              'lng': sites[0].longitude
            },
            // setting visible false since the icon will be the poi icon
            visible: false
          }).then((marker: Marker) => {
    
            this.htmInfoWindow.open(marker);
          });
    
    0 讨论(0)
  • 2021-01-06 00:23

    If the position is set, there is no problem showing the info window

    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 6,
      center: {lat: 55.6761, lng: 12.5683},
      mapTypeId: google.maps.MapTypeId.TERRAIN
    });
    var infowindow = new google.maps.InfoWindow({
      content: "Copenhagen"
    });
    infowindow.setPosition({lat: 55.6761, lng: 12.5683});
    infowindow.open(map);
    }
    
    0 讨论(0)
提交回复
热议问题