Google Maps Javascript API以小方块显示地图

时间:2014-05-09 13:28:53

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

我已经实施了Google地图JavaScript API,我正在尝试获取全屏幕地图。

地图本身只显示在角落的一个小方框中,我知道容器的大小合适,因为我在每个角落都有谷歌元素,但地图并没有填充容器。

这是我的所作所为: Screenshot Of Map

代码:

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAh_6k9o89NOW69iR9mL_0J6vu9QMm_54w&sensor=false&extension=.js"></script> 
<script> google.maps.event.addDomListener(window, 'load', init);

var map;

function init() {
    var mapOptions = {
        center: new google.maps.LatLng(51.508800670335646,-0.06943131238223),
        zoom: 12,
        zoomControl: true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.DEFAULT,
        },
        disableDoubleClickZoom: true,
        mapTypeControl: true,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
        },
        scaleControl: true,
        scrollwheel: true,
        streetViewControl: true,
        draggable : true,
        overviewMapControl: true,
        overviewMapControlOptions: {
            opened: false,
        },
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    styles: [
    {
        featureType: 'water',
        stylers: [{color:'#46bcec'},{visibility:'on'}]
    },{
        featureType: 'landscape',
        stylers: [{color:'#f2f2f2'}]
    },{
        featureType: 'road',
        stylers: [{saturation: -100},{lightness: 45}]
    },{
        featureType: 'road.highway',
        stylers: [{visibility: 'simplified'}]
    },{
        featureType: 'road.arterial',
        elementType: 'labels.icon',
        stylers: [{visibility: 'off'}]
    },{
        featureType: 'administrative',
        elementType: 'labels.text.fill',
        stylers: [{color: '#444444'}]
    },{
        featureType: 'transit',
        stylers: [{visibility: 'off'}]
    },{
        featureType: 'poi',
        stylers: [{visibility: 'off'}]
    }
],

    }

    var mapElement = document.getElementById('map');
    var map = new google.maps.Map(mapElement, mapOptions);
    var locations = [

    ];

    for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            icon: '',
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });
    }
}
</script> 

<style> 
#map{
    width:100%;
    height:100%;
}
</style> 

非常感谢您的意见。

2 个答案:

答案 0 :(得分:2)

您的地图容器正变为100%的宽度和高度。

试试这个......

<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map {
    width: 100%;
    height: 100%;
}
</style>

答案 1 :(得分:1)

尝试添加

html { height: 100% } body { height: 100%; margin: 0px; padding: 0px }

样式表/ css中的

jsfiddle示例:http://jsfiddle.net/lotusgodkk/SD9km/1/