添加InfoBox谷歌地图

时间:2014-03-21 12:20:40

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

我试图在我的谷歌地图中加入一个信息框:我有以下代码

function initializeSwansea() {

  var latlng = new google.maps.LatLng(51.656591, -3.902619);

    var styles = [
                    {
                        featureType: 'landscape.natural',
                        elementType: 'all',
                        stylers: [
                            { hue: '#ffffff' },
                            { saturation: -100 },
                            { lightness: 100 },
                            { visibility: 'on' }
                        ]
                    },{
                        featureType: 'water',
                        elementType: 'geometry',
                        stylers: [
                            { hue: '#93ddf6' },
                            { saturation: 72 },
                            { lightness: 4 },
                            { visibility: 'simplified' }
                        ]
                    },{
                        featureType: 'landscape.man_made',
                        elementType: 'all',
                        stylers: [
                            { hue: '#ffffff' },
                            { saturation: -100 },
                            { lightness: 100 },
                            { visibility: 'simplified' }
                        ]
                    },{
                        featureType: 'road',
                        elementType: 'all',
                        stylers: [
                            { hue: '#999999' },
                            { saturation: -100 },
                            { lightness: -6 },
                            { visibility: 'simplified' }
                        ]
                    },{
                        featureType: 'poi',
                        elementType: 'labels',
                        stylers: [
                            { hue: '#a7a9ac' },
                            { saturation: -93 },
                            { lightness: -15 },
                            { visibility: 'simplified' }
                        ]
                    },{
                        featureType: 'poi',
                        elementType: 'geometry',
                        stylers: [
                            { hue: '#80c342' },
                            { saturation: 15 },
                            { lightness: -34 },
                            { visibility: 'on' }
                        ]
                    },{
                        featureType: 'administrative.country',
                        elementType: 'labels',
                        stylers: [
                            { hue: '#0054a6' },
                            { saturation: 100 },
                            { lightness: -36 },
                            { visibility: 'on' }
                        ]
                    },{
                        featureType: 'road.highway',
                        elementType: 'labels',
                        stylers: [
                            { hue: '#999999' },
                            { saturation: -100 },
                            { lightness: -6 },
                            { visibility: 'on' }
                        ]
                    }
                ];

    var mapOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: 'Styled',
        mapTypeControl: false,
        streetViewControl: false,
        zoomControl: true,
        scaleControl: true,
        mapTypeControlOptions: {
            mapTypeIds: [ 'Styled']
          }
    };

    var boxText = document.createElement("div");
    boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";
    boxText.innerHTML = "City Hall, Sechelt<br>British Columbia<br>Canada";

    var myOptions = {
         content: boxText
        ,disableAutoPan: false
        ,maxWidth: 0
        ,pixelOffset: new google.maps.Size(-140, 0)
        ,zIndex: null
        ,boxStyle: { 
          background: "url('tipbox.gif') no-repeat"
          ,opacity: 0.75
          ,width: "280px"
         }
        ,closeBoxMargin: "10px 2px 2px 2px"
        ,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
        ,infoBoxClearance: new google.maps.Size(1, 1)
        ,isHidden: false
        ,pane: "floatPane"
        ,enableEventPropagation: false
    };

    var ib = new InfoBox(myOptions);

    var map = new google.maps.Map(document.getElementById("map-swansea"), mapOptions);


    var swansea = new google.maps.Marker({
        position: new google.maps.LatLng(51.651532, -3.913906),
        map: map,
        title: "Swansea"
    });


    var styledMapType = new google.maps.StyledMapType(styles, { name: 'Styled' });
map.mapTypes.set('Styled', styledMapType);

    $(window).resize( function() { map.setCenter(latlng) });
}

$(function() {
    initializeSwansea();
});

我基本上希望在地图加载时显示包含内容,地址详细信息等的标签。我已阅读此link,但仍不确定如何将其合并到我的代码中?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

代码没问题。你基本上只需要显示信息框:

google.maps.event.addListener(swansea, 'click', function() {
   ib.open(map, swansea);
});

在这个小提琴中看到你的代码 - &gt; http://jsfiddle.net/esCeX/ (还有其他一些小改动)