我正在尝试使用信息框和Gmaps.js在Google地图中显示弹出窗口但是Window不会在标记点击时打开?

时间:2016-10-16 13:29:46

标签: google-maps infobox

我正在尝试使用信息框显示弹出框,但是信息框没有打开我正在使用Gmaps.js脚本,Map正在加载内容完美但只有信息框的问题。 这是我的代码:

map = new GMaps({
    div: '#gmap_geocoding',
    lat: "20.5937",
    lng: "78.9629",
    zoom:5
});            
var icon = {
    url: "/img/marker/location-pointer.png", // url
    scaledSize: new google.maps.Size(40, 40), // scaled size
    origin: new google.maps.Point(0,0), // origin
    anchor: new google.maps.Point(0, 0) // anchor
};    
var infoBox;
var boxOptions = {
    disableAutoPan: true,
    alignBottom: true,
    closeBoxURL: "",
    maxWidth: 0,  // no max
    pixelOffset: new google.maps.Size(-140, -12), 
    infoBoxClearance: new google.maps.Size(1, 1) 
};
infoBox  = new InfoBox(boxOptions);
$results = $('#search-results').find('div.MapMarker');
$.each($results, function(key,value){
    var lat = $(value).data('lat');
    var lng = $(value).data('lng');
    var name = $(value).data('name');
    var sport = $(value).data('sport');
    var image = $(value).data('image');
    var contentString =
        "<div class='infoWindow text-center'><h4><a href='{{ url('/activity/') }}/{{ $vendor->vendor_slug }}/{{ $vendor->activity_slug }}'>"+ name +"</a></h4><img src='"+ image +" 'class='img-responsive' style='width:220px; height:160px;'>"+            
                "<h5><strong>Sports:</strong> "+ sport + "</h5></div>";        

    var marker = map.addMarker({
        lat:lat,
        lng:lng,
        icon: icon,
        animation: google.maps.Animation.DROP,
        title: name,
        infoBox:{
            content:contentString
        }
 });                                
});

1 个答案:

答案 0 :(得分:1)

  1. 您需要包含InfoBox外部库(您可能正在这样做,但您的问题并不清楚)。

  2. 您需要向标记添加点击事件监听器以打开InfoBox。

  3.    var marker = map.addMarker({
           lat:lat,
           lng:lng,
           icon: icon,
           animation: google.maps.Animation.DROP,
           title: name,
           click: function(evt) {
             infoBox.setContent(contentString);
             infoBox.open(map.map, marker);
           },
           infoBox:{ // probably not required
               content:contentString
           }
    });     
    

    proof of concept fiddle

    代码段

    var map;
    map = new GMaps({
      div: '#gmap_geocoding',
      lat: "20.5937",
      lng: "78.9629",
      zoom: 5
    });
    var icon = {
      url: "/img/marker/location-pointer.png", // url
      scaledSize: new google.maps.Size(40, 40), // scaled size
      origin: new google.maps.Point(0, 0), // origin
      anchor: new google.maps.Point(0, 0) // anchor
    };
    var infoBox;
    var boxOptions = {
      disableAutoPan: true,
      alignBottom: true,
      closeBoxURL: "",
      maxWidth: 0, // no max
      pixelOffset: new google.maps.Size(-140, -12),
      infoBoxClearance: new google.maps.Size(1, 1),
      boxStyle: {
        opacity: 0.75,
        width: "280px",
        backgroundColor: "white",
        border: "black 1px solid"
      }
    };
    infoBox = new InfoBox(boxOptions);
    $results = $('#search-results').find('div.MapMarker');
    $.each($results, function(key, value) {
      var lat = $(value).data('lat');
      var lng = $(value).data('lng');
      var name = $(value).data('name');
      var sport = $(value).data('sport');
      var image = $(value).data('image');
      var contentString =
        "<div class='infoWindow text-center'><h4><a href='{{ url('/activity/') }}/{{ $vendor->vendor_slug }}/{{ $vendor->activity_slug }}'>" + name + "</a></h4><img src='" + image + " 'class='img-responsive' style='width:220px; height:160px;'>" +
        "<h5><strong>Sports:</strong> " + sport + "</h5></div>";
    
      var marker = map.addMarker({
        lat: lat,
        lng: lng,
        // icon: icon,
        animation: google.maps.Animation.DROP,
        title: name,
        click: function(evt) {
          infoBox.setContent(contentString);
          infoBox.open(map.map, marker);
        },
        infoBox: {
          content: contentString
        }
      });
    });
    html,
    body,
    #gmap_geocoding {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gmaps.js/0.4.12/gmaps.min.js"></script>
    <script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js"></script>
    <div id="gmap_geocoding"></div>
    <div id="search-results">
      <div class="MapMarker" data-lat="20.5937" data-lng="78.9629" data-name="name" data-sport="baseball" data-image="http://images.clipartpanda.com/batter-clipart-silhouette_of_a_batter_swinging_a_baseball_bat_0515-0902-1110-5634_SMU.jpg" />
    </div>