打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Google Map API 信息窗口

InfoWindow 在地图上方的浮动窗口中显示内容。信息窗口有点像漫画书上的文字气球,它有一个内容区域和锥形引线,引线的头位于地图的指定位置上。通过点击 Google Maps 上的商户标记,您可以看到活动的信息窗口。

InfoWindow 构造函数采用的是 InfoWindow options 对象,该对象指定了用于显示信息窗口的一组初始参数。在创建信息窗口的过程中,系统不会在地图上添加信息窗口。要显示信息窗口,您需要调用 InfoWindow 上的 open() 方法,向其传递要在其中打开信息窗口的 Map,以及向其传递用于锚定信息窗口的 Marker(可选)。(如果未提供任何标记,那么,会在其 position 属性上打开信息窗口)。

InfoWindow options 对象是包含以下字段的对象常量:

  • content 包含了信息窗口打开时,系统要在其中显示的文本字符串或 DOM 节点。
  • pixelOffset 包含了从信息窗口的顶部到信息窗口锚定位置的偏移量。实际上,您不应也无需修改此字段。
  • position 包含了此信息窗口锚定位置的 LatLng。请注意,在标记上执行打开信息窗口操作时,系统会自动使用一个新位置更新该值。
  • maxWidth 指定了信息窗口的最大宽度(以像素为单位)。默认情况下,信息窗口会根据其中包含的内容进行扩展,如果信息窗口随着地图的大小而扩展,那么,文本将会自动换行。如果您应用了 maxWidth,那么,信息窗口将自动换行以强制适应像素的宽度。如果屏幕的实际使用面积允许的话,信息窗口在达到最大宽度后仍可垂直扩展。

InfoWindow 的内容可以是文本字符串、HTML 代码段或 DOM 元素本身。要设置此内容,请在InfoWindow options 构造函数中传递该内容,或者对信息窗口显式调用 setContent()。如果想要显式调整内容的大小,您可以使用 <div> 进行此操作,如果您希望,还可以启用滚动功能。请注意,如果您没有启用滚动功能,而内容的大小又超过了信息窗口的可用空间,那么,内容可能会从信息窗口中“溢”出。

InfoWindow 可附加到 Marker 对象(在这种情况下,它们的位置取决于标记的位置)上,或附加到地图本身指定的 LatLng 位置上。如果您一次只想显示一个信息窗口(正如 Google Maps 上的相应行为),那么,您只需创建一个信息窗口,然后在地图事件(例如用户点击)执行过程中将此信息窗口重新分配到不同的位置或标记中。但与 Google Maps API 第 2 版中的相应行为不同的是,如果您选择进行上述操作,那么,地图可能会立即显示多个 InfoWindow 对象。

要更改信息窗口的位置,您可以对信息窗口调用 setPosition() 以显式的方式更改其位置,或者使用 InfoWindow.open() 方法将信息窗口附加到新标记上。请注意,如果您在没有传递标记的情况下调用了 open(),那么,InfoWindow 将会使用在构建过程中通过 InfoWindow options 对象指定的位置。

以下代码显示了澳大利亚中心位置的标记。点击该标记可显示信息窗口。

代码片段(3)[全屏查看所有代码]

1. [代码]js代码     

01var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
02var myOptions = {
03  zoom: 4,
04  center: myLatlng,
05  mapTypeId: google.maps.MapTypeId.ROADMAP
06}
07 
08var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
09 
10var contentString = '<div id="content">'+
11    '<div id="siteNotice">'+
12    '</div>'+
13    '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
14    '<div id="bodyContent">'+
15    '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
16    'sandstone rock formation in the southern part of the '+
17    'Northern Territory, central Australia. It lies 335 km (208 mi) '+
18    'south west of the nearest large town, Alice Springs; 450 km '+
19    '(280 mi) by road. Kata Tjuta and Uluru are the two major '+
20    'features of the Uluru - Kata Tjuta National Park. Uluru is '+
21    'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
22    'Aboriginal people of the area. It has many springs, waterholes, '+
23    'rock caves and ancient paintings. Uluru is listed as a World '+
24    'Heritage Site.</p>'+
25    '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
26    'http://en.wikipedia.org/w/index.php?title=Uluru</a> (last visited June 22, 2009).</p>'+
27    '</div>'+
28    '</div>';
29 
30var infowindow = new google.maps.InfoWindow({
31    content: contentString
32});
33 
34var marker = new google.maps.Marker({
35    position: myLatlng,
36    map: map,
37    title:"Uluru (Ayers Rock)"
38});
39 
40google.maps.event.addListener(marker, 'click'function() {
41  infowindow.open(map,marker);
42});

2. [文件] infowindow-simple.html ~ 2KB     下载(244)     

01<!DOCTYPE html>
02<html>
03<head>
04<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
05<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
06<title>Google Maps JavaScript API v3 Example: Info Window Simple</title>
07<linkhref="http://code.google.com/apis/maps/documentation/javascript/examples/default.css"rel="stylesheet" type="text/css" />
08<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
09<script type="text/javascript">
10  function initialize() {
11    var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
12    var myOptions = {
13      zoom: 4,
14      center: myLatlng,
15      mapTypeId: google.maps.MapTypeId.ROADMAP
16    }
17 
18    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
19 
20    var contentString = '<div id="content">'+
21        '<div id="siteNotice">'+
22        '</div>'+
23        '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
24        '<div id="bodyContent">'+
25        '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
26        'sandstone rock formation in the southern part of the '+
27        'Northern Territory, central Australia. It lies 335 km (208 mi) '+
28        'south west of the nearest large town, Alice Springs; 450 km '+
29        '(280 mi) by road. Kata Tjuta and Uluru are the two major '+
30        'features of the Uluru - Kata Tjuta National Park. Uluru is '+
31        'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
32        'Aboriginal people of the area. It has many springs, waterholes, '+
33        'rock caves and ancient paintings. Uluru is listed as a World '+
34        'Heritage Site.</p>'+
35        '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
36        'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
37        '(last visited June 22, 2009).</p>'+
38        '</div>'+
39        '</div>';
40         
41    var infowindow = new google.maps.InfoWindow({
42        content: contentString
43    });
44 
45    var marker = new google.maps.Marker({
46        position: myLatlng,
47        map: map,
48        title: 'Uluru (Ayers Rock)'
49    });
50    google.maps.event.addListener(marker, 'click', function() {
51      infowindow.open(map,marker);
52    });
53  }
54 
55</script>
56</head>
57<body onload="initialize()">
58  <div id="map_canvas"></div>
59</body>
60</html>

3. [文件] infowindow-simple-max.html ~ 2KB     下载(172)     跳至 [1] [2] [3] [全屏预览]

01<!DOCTYPE html>
02<html>
03<head>
04<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
05<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
06<title>Google Maps JavaScript API v3 Example: Info Window Simple</title>
07<linkhref="http://code.google.com/apis/maps/documentation/javascript/examples/default.css"rel="stylesheet" type="text/css" />
08<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
09<script type="text/javascript">
10  function initialize() {
11    var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
12    var myOptions = {
13      zoom: 4,
14      center: myLatlng,
15      mapTypeId: google.maps.MapTypeId.ROADMAP
16    }
17 
18    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
19 
20    var contentString = '<div id="content">'+
21        '<div id="siteNotice">'+
22        '</div>'+
23        '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
24        '<div id="bodyContent">'+
25        '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
26        'sandstone rock formation in the southern part of the '+
27        'Northern Territory, central Australia. It lies 335 km (208 mi) '+
28        'south west of the nearest large town, Alice Springs; 450 km '+
29        '(280 mi) by road. Kata Tjuta and Uluru are the two major '+
30        'features of the Uluru - Kata Tjuta National Park. Uluru is '+
31        'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
32        'Aboriginal people of the area. It has many springs, waterholes, '+
33        'rock caves and ancient paintings. Uluru is listed as a World '+
34        'Heritage Site.</p>'+
35        '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
36        'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
37        '(last visited June 22, 2009).</p>'+
38        '</div>'+
39        '</div>';
40         
41    var infowindow = new google.maps.InfoWindow({
42        content: contentString,
43        maxWidth: 200
44    });
45 
46    var marker = new google.maps.Marker({
47        position: myLatlng,
48        map: map,
49        title: 'Uluru (Ayers Rock)'
50    });
51    google.maps.event.addListener(marker, 'click', function() {
52      infowindow.open(map,marker);
53    });
54  }
55 
56</script>
57</head>
58<body onload="initialize()">
59  <div id="map_canvas"></div>
60</body>
61</html>
举报
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
离线地图制作工具
javascript – Kendo UI和Google Maps
Google Map JavaScript API v3 常用功能总结
html5 navigator.geolocation基于浏览器获取地理位置
基于ip的手机地理定位
百度地图 点击和标注拖拽后的经纬度
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服