javascript 如何在 gmaps v0.3 中自动打开 infoWindow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15106956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:31:33  来源:igfitidea点击:

How auto open infoWindow in gmaps v0.3

javascriptjqueryhtmlgoogle-mapsgoogle-maps-api-3

提问by daiguachen

I have a problem about gmaps v0.3 that when I addMarker How let infoWindow auto open. not when you onclick open.

我有一个关于 gmaps v0.3 的问题,当我 addMarker 如何让 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);
 });

I want to when addMarker auto open infoWindow not click, what should I do. please help me.

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

回答by Tommy S?rensen

You can open the infoWindow by using the .openfunction:

您可以使用以下.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);

You can read about infoWindow at the Google Maps website https://developers.google.com/maps/documentation/javascript/overlays#InfoWindows

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

回答by daan.desmedt

Every marker added to the Gmaps.map intance has it's own InfoWindow stored into the markers object. We need the Array index key to that specific marker. Adress by the index key the right InfoWinow o be opened. You can open the GMaps.js specific marker by doing following:

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

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

Replace [index] by the index of the marker you want the InfoWindow to open.

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

回答by JuKe

use the events from google.maps.Marker ('Events' section)

使用google.maps.Marker 中的事件(“事件”部分)

example:

例子:

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();
    }
});