Google Maps API v3:对多个地址和信息窗口进行地理编码 [英] Google maps api v3: geocoding multiple addresses and infowindow

查看:46
本文介绍了Google Maps API v3:对多个地址和信息窗口进行地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取多个地址的信息窗口.它正在创建标记,但是当我单击标记时,不会弹出信息窗口.请帮助,看看这段代码可能有什么问题.休息所有信息都很好,唯一的问题是没有出现信息窗口.

I am trying to get infowindow for multiple addresses. Its creating markers but when I click on markers, infowindow is not popping up. Please help and see what could be wrong in this code. Rest all info is fine only issue is with infowindow not coming up.

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="height: 800px;"></div>

  <script type="text/javascript">
    var locations = [
      ['Bondi Beach', '850 Bay st 04 Toronto, Ont'],
      ['Coogee Beach', '932 Bay Street, Toronto, ON M5S 1B1'],
      ['Cronulla Beach', '61 Town Centre Court, Toronto, ON M1P'],
      ['Manly Beach', '832 Bay Street, Toronto, ON M5S 1B1'],
      ['Maroubra Beach', '606 New Toronto Street, Toronto, ON M8V 2E8']
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(43.253205,-80.480347),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();
    var geocoder = new google.maps.Geocoder();

    var marker, i;

    for (i = 0; i < locations.length; i++) {

        geocoder.geocode( { 'address': locations[i][1]}, function(results, status) {
            //alert(status);
            if (status == google.maps.GeocoderStatus.OK) {

                //alert(results[0].geometry.location);
                map.setCenter(results[0].geometry.location);
                marker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map
                }); 

                google.maps.event.addListener(marker, 'mouseover', function() { infowindow.open(marker, map);});
                google.maps.event.addListener(marker, 'mouseout', function() { infowindow.close();});

            }
            else
            {
                alert("some problem in geocode" + status);
            }
        }); 
    }

  </script>
</body>
</html>

推荐答案

您向后具有google.maps.InfoWindow.open方法的参数:

You have the arguments to the google.maps.InfoWindow.open method backwards:

打开(地图?:地图| StreetViewPanorama,锚点?:MVCObject)|无|在给定的地图上打开此InfoWindow.(可选)InfoWindow可以与锚关联.在核心API中,唯一的锚点是Marker类.但是,锚点可以是任何公开LatLng位置属性的MVCObject,还可以是用于显示pixelOffset的Point anchorPoint属性(请参见InfoWindowOptions).anchorPoint是从锚点位置到InfoWindow尖端的偏移量.

open(map?:Map|StreetViewPanorama, anchor?:MVCObject) | None | Opens this InfoWindow on the given map. Optionally, an InfoWindow can be associated with an anchor. In the core API, the only anchor is the Marker class. However, an anchor can be any MVCObject that exposes a LatLng position property and optionally a Point anchorPoint property for calculating the pixelOffset (see InfoWindowOptions). The anchorPoint is the offset from the anchor's position to the tip of the InfoWindow.

google.maps.event.addListener(marker, 'mouseover', function() { infowindow.open(marker, map);});

应该是:

google.maps.event.addListener(marker, 'mouseover', function() { infowindow.open(map, marker);});

工作示例

这篇关于Google Maps API v3:对多个地址和信息窗口进行地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆