【问题标题】:How auto open infoWindow in gmaps v0.3如何在 gmaps v0.3 中自动打开 infoWindow
【发布时间】:2013-02-12 22:58:03
【问题描述】:

我有一个关于 gmaps v0.3 的问题,当我添加标记时如何让 infoWindow 自动打开。不是当你点击打开时。

<script type="text/javascript">
var map;
$(document).ready(function(){
    map = new GMaps({
        div: '#map',
        lat: 39.908403,
        lng: 116.397529,
        zoom: 1,
    });
    var marker = new google.maps.Marker();
    marker = {
        lat: 39.908403,
        lng: 116.397529,
        title: 'Lima',
        //map: map.map,
        //animation: google.maps.Animation.BOUNCE,
        //shape: {coords: [0,0,50,50], type: "rect"},
        infoWindow: {
          content: '<font color="red">hello world</font>'
        }
    }
    map.addMarker(marker);
 });

我想当addMarker 自动打开infoWindow 没有点击时,我该怎么办。 请帮帮我。

【问题讨论】:

  • 什么是“在这里输入代码”?请发布完整的功能。
  • 对不起!我已经修改了,现在是最终代码。我该怎么办

标签: javascript jquery html google-maps google-maps-api-3


【解决方案1】:

您可以使用.open 函数打开信息窗口:

// Create map
var map = new GMaps({
    div: '#map',
    lat: 39.908403,
    lng: 116.397529,
    zoom: 1,
});

// Create infoWindow
var infoWindow = new google.maps.InfoWindow({
    content: 'Content goes here..'
});

// Create marker
var marker = new google.maps.Marker({
    lat: lat,
    lng: lng,
    title: 'Lima',
    map: map.map,
    infoWindow: infoWindow
});

// This opens the infoWindow
infoWindow.open(map, marker);

您可以在 Google 地图网站https://developers.google.com/maps/documentation/javascript/overlays#InfoWindows 上阅读有关 infoWindow 的信息

【讨论】:

  • 我试了一下,地图上没有标记。现在,代码可以运行了,但是我想在程序运行时显示infoWindow,点击标记后不显示infoWindow
【解决方案2】:

添加到 Gmaps.map 实例的每个标记都有自己的 InfoWindow 存储到标记对象中。我们需要该特定标记的数组索引键。地址按索引键右边的InfoWinow o 被打开。您可以通过执行以下操作打开 GMaps.js 特定标记:

(map.markers[index].infoWindow).open(map.map,map.markers[index]);

将 [index] 替换为您希望信息窗口打开的标记的索引。

【讨论】:

    【解决方案3】:

    使用来自google.maps.Marker ('Events' section)的事件

    示例:

    map.addMarker({
        lat: 50.17222520000001,
        lng: 12.196652600000002,
        infoWindow: {
            content: '<p>Foobar</p>'
        },
        mouseover: function(){
            (this.infoWindow).open(this.map, this);
        },
        mouseout: function(){
            this.infoWindow.close();
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 2015-09-18
      • 1970-01-01
      • 2013-03-31
      • 2018-08-03
      相关资源
      最近更新 更多