google-maps - 有没有办法将 id 添加到将标记添加到谷歌地图时创建的 div 中?

标签 google-maps

这个问题是不言自明的。当标记添加到谷歌地图时,在 DOM 中,它看起来实际上是一个包裹在标记图像周围的 div。如果这些 div 可以被唯一标识,那么对我来说会容易得多,例如我可以使用 getElementById() 或类似方法抓取 map 上的各个标记。我正在使用谷歌地图 v3。

谢谢。

最佳答案

是的 - 您可以创建自定义 Overlay 类来包含自定义标记。幸运的是,有人已经有了这个想法并且做到了。查看 Google Map Utility Libraries 中的 RichMarker 和 StyledMarker (在页面底部)两者都允许您将自定义标记/CSS 类放入标记标记中。

如果您只想将工具提示附加到标记,您也可以在创建时简单地将自定义属性附加到标记(例如工具提示文本或其他内容),然后添加鼠标悬停事件处理程序来读取该属性并对其执行某些操作.

示例:

    <html> 
    <head> 

    <meta name="viewport" content="initial-scale=1.0, user-scalable=yes" /> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps Markers</title> 
    <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 
    var map;
    //your tooltips
    msg = ['Yeah Right', 'oompa oompa','I feel good']
    var marker;
    function initialize() {
      var chicago = new google.maps.LatLng(41.875696,-87.624207);
      var myOptions = {
        zoom: 11,
        center: chicago,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);

    marker = new google.maps.Marker({
    map:map,
    position: chicago,
    title: "<h2>Sample Tooltip</h2>", //title can also be used for custom tooltips
    myCustomIndex:2 //storing index of a tooltip
    })


    //if you have multiple markers pls read about event closures http://code.google.com/apis/maps/documentation/javascript/events.html#EventClosures

//attach an event to the marker
     google.maps.event.addListener(marker, 'mouseover', function() {
        //you could also use the title property of the marker to hold the text of the tooltop
        showFakeTooltip(marker.title)
        //or you could show it by accessing a custom property
        showTooltip(marker.myCustomIndex);
      });
    }

    function showFakeTooltip(title) {
    $(".fakeTooltip").html(title);
    }

    function showTooltip(index) {
    alert(msg[index])
    }




    </script> 
    </head> 
    <body onload="initialize()"> 

      <div id="map_canvas" style="width: 900px;height: 600px;"></div> 
    <div class="fakeTooltip"></div>
    </body> 
    </html>

关于google-maps - 有没有办法将 id 添加到将标记添加到谷歌地图时创建的 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4869869/

相关文章:

Android 在带有方向路径的谷歌地图上绘制带箭头的多段线

php - 获取 Google API 数据时 cURL 不起作用

javascript - 向谷歌地图 API 添加点

java - 在android中找不到JSON解析运行时错误源

c# - 如何使用 C# 从文件中读取内容并将其用于 ASP.NET 页面上的 HTML 代码?

ios - 带有谷歌地图的应用程序将在 ios6 中运行

api - 明确 API 的含义

javascript - 如何让 Google map 在 Visual Studio Web 表单中工作?

java - 无法使用谷歌地图构建应用程序

javascript - Google maps V3 用鼠标右键拖动 map ?