【问题标题】:How to target an element outside map by clicking on google maps marker如何通过点击谷歌地图标记来定位地图外的元素
【发布时间】:2017-12-30 10:34:26
【问题描述】:

我有一组文章链接到谷歌地图上的标记。我正在使用 Laravel 5 和谷歌地图 API。当我点击一篇文章时,它的信息 div 会展开,谷歌地图信息窗口也会展开。

我想要做的是点击一个谷歌地图标记并展开文章信息 div。即我希望它能够双向工作。

文章是动态创建的。

这是我的谷歌地图代码的相关部分:

    var barID = [
    @foreach($articles as $article)
    {{$article->id}},
    @endforeach
    ];
for (i = 0; i < locations.length; i++) {
    infoWindowContent[i] = getInfoWindowDetails(locations[i]);
    var location = new google.maps.LatLng(locations[i][0], locations[i][1]);
    var marker = new google.maps.Marker({
        position: location,
        map: map,
        id: barID[i],
        icon: 'images/happymarker.png'
    });
  var infoBubble = new InfoBubble({
      map: map, etc..
    });
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infoBubble.setContent(infoWindowContent[i]);
          infoBubble.open(map, marker);
          map.panTo(marker.getPosition());
          map.setZoom(17);
        }
      })(marker, i));
      markers.push(marker);
}
// SHOW MORE INFO BUTTON TRIGGERS INFOWINDOW/MARKER
    function myClick(id){
        google.maps.event.trigger(markers[id], 'click');
    } 

这是我的尝试 - 但它不起作用。

marker.addListener('click', function(theID) {

$(".descriptionTextContainer" + theID).show();  

      });

标记/文章(地图容器外)。我已经为所有文章添加了一个 id,因此它将是 class="descriptionTextContainer 24" - 即所有不同的文本容器在它们的类中都有唯一的 id。

 <article class="barContainer mix category-<?php echo $article->id; ?>" data-rating="<?php echo $article->rating; ?>" data-distance="<?php echo $article->distance; ?>">
 $something = $articles->lists('id'); 
                    $artID = $article->id;  
                    $key= $something->search($artID); ?>   

// THIS IS THE PLACE WHERE I CLICK TO SHOW THE 'descriptionTextContainer'               
  <div class="resultsTitle showMoreDetails" onclick="myClick(<?php echo $key; ?>);">
    <div class="barTitleContainer"><h2>{{$article->title}}</h2></div>
  </div>           
  <div class="descriptionTextContainer <?php echo $key; ?>">            
// THIS IS HIDDEN TILL THE MARKER OR THE 'ShowMoreDetails' is clicked                  
  </div>
</article>

这里是the site,因为它正在使用 atm。

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 laravel-5


    【解决方案1】:

    您的标记已经绑定:您可以通过在 id 上添加一行来打开 div,因为您已将文章 id 存储在 marker.id 中

    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infoBubble.setContent(infoWindowContent[i]);
          infoBubble.open(map, marker);
          map.panTo(marker.getPosition());
          map.setZoom(17);
          // Add this line
          $(".descriptionTextContainer" + marker.id).show(); 
        }
      })(marker, i));
    

    【讨论】:

    • 感谢您的帮助!不幸的是,这似乎没有任何作用。如果我 console.log(marker.id) 它只显示所有 ID 的数组,而不是我试图定位的特定 ID。想法?
    • 如果我在 gmaps 侦听器中使用 console.log(marker.barID[i]),我似乎能够获得我想要的 ID。但是我在 [i] 之前放什么并不重要,我总是得到同样的错误:“Uncaught TypeError: Cannot read property 'the ID i'm looking for' of undefined”
    • 在您创建标记之前,在var marker = new google.maps.Marker({ position: location, map: map, id: barID[i], icon: 'images/happymarker.png' }); 上,您可以console.log(barID[i]) ; 吗?这里肯定有问题...如果您声明 id:barId[i] 那么 marker.id 就是您需要的 id,不明白为什么您在侦听器中获得了整个数组。您还可以在创建标记时添加 articleId : barID[i] 而不是 id : barID[i],以确保密钥仅用于此目的。
    • 谢谢!做到了。干杯。 $(".descriptionTextContainer" + "." + marker.articleID).show();
    • 很高兴它有效;)您可以将问题标记为已回答。
    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    相关资源
    最近更新 更多