首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google地图自动设置位置:#map div的相对位置

问Google地图自动设置位置:#map div的相对位置
EN

Stack Overflow用户
提问于 2016-10-24 17:03:39
回答 2查看 10.5K关注 0票数 3

我有个奇怪的问题,至少对我来说有点奇怪。当我设置#map div的样式并设置位置:div(因为我需要它作为背景)时,它会自动将位置设置为相对位置,并覆盖我的设置。我不知道该在哪里改变这种行为。

父类:

代码语言:javascript
复制
.wrapper {
  margin: 0;
  padding: 0;
  height: 100vh;
  z-index: 1;
  display: flex;
}

和#地图

代码语言:javascript
复制
 #map {
      height: 100%;
      width: 100vw;
      overflow: hidden;
      z-index: 0;
      top: 0;
      left: 0;
      position: fixed;

代码很简单:

代码语言:javascript
复制
<div class="wrapper">
       <div id="map"></div>
        <div class="entries container">
            {% with 940 as width %}{% placeholder content %}{% endwith %}
            </p>
        </div>
    </div>

现在,当我输入页面时,它首先很好地呈现,在一个milisec应用js之后,我假设#map div添加了以下行

代码语言:javascript
复制
element.style {
    position: relative;
    overflow: hidden;
}

好吧,如果我手动修改它,它就能实现我想要的结果,但是我不能禁用这个覆盖。还有其他人为这种事辩解过吗?我有点卡住了。

我在看Chrome。

提前谢谢你的时间!问候

JS文件:

代码语言:javascript
复制
var map; 
function initialize() {
    var v = {lat: 4.00, lng: 7.00};

    var marker = new google.maps.Marker({
        position: v,
        map: map,
        title: 'V!'
    });

    var styleArray = [
        {
            "featureType": "all",
            "elementType": "all",
            "stylers": [
                {
                    "hue": "#ff0000"
                },
                {
                    "saturation": -100
                },
                {
                    "lightness": -30
                }
            ]
        },
        {
            "featureType": "all",
            "elementType": "labels.text.fill",
            "stylers": [
                {
                    "color": "#ffffff"
                }
            ]
        },
        {
            "featureType": "all",
            "elementType": "labels.text.stroke",
            "stylers": [
                {
                    "color": "#353535"
                }
            ]
        },
        {
            "featureType": "landscape",
            "elementType": "geometry",
            "stylers": [
                {
                    "color": "#656565"
                }
            ]
        },
        {
            "featureType": "poi",
            "elementType": "geometry.fill",
            "stylers": [
                {
                    "color": "#505050"
                }
            ]
        },
        {
            "featureType": "poi",
            "elementType": "geometry.stroke",
            "stylers": [
                {
                    "color": "#808080"
                }
            ]
        },
        {
            "featureType": "road",
            "elementType": "geometry",
            "stylers": [
                {
                    "color": "#454545"
                }
            ]
        }
    ];
    var mapOptions = {
        center: new google.maps.LatLng(47.295065, 7.937821),
        zoom: 14,
        zoomControl: true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL,
        },
        disableDoubleClickZoom: false,
        mapTypeControl: false,
        scaleControl: false,
        scrollwheel: false,
        panControl: false,
        streetViewControl: false,
        draggable: false,
        overviewMapControl: false,
        overviewMapControlOptions: {
            opened: false,
        },
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: styleArray
    };
    map = new google.maps.Map(document.getElementById("map"),
        mapOptions);

    var locations = [
        ['V', 'undefined', 'undefined', 'undefined', 'undefined', 4.0, 7.0, 'https://mapbuildr.com/assets/img/markers/hollow-pin-red.png']
    ];
    for (i = 0; i < locations.length; i++) {
        if (locations[i][1] == 'undefined') {
            description = '';
        } else {
            description = locations[i][1];
        }
        if (locations[i][2] == 'undefined') {
            telephone = '';
        } else {
            telephone = locations[i][2];
        }
        if (locations[i][3] == 'undefined') {
            email = '';
        } else {
            email = locations[i][3];
        }
        if (locations[i][4] == 'undefined') {
            web = '';
        } else {
            web = locations[i][4];
        }
        if (locations[i][7] == 'undefined') {
            markericon = '';
        } else {
            markericon = locations[i][7];
        }
        marker = new google.maps.Marker({
            icon: markericon,
            position: new google.maps.LatLng(locations[i][5], locations[i][6]),
            map: map,
            title: locations[i][0],
            desc: description,
            tel: telephone,
            email: email,
            web: web
        });
        link = '';
    }

    map.panBy(0, 200);

}

google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addDomListener(window, "resize", function () {
    var center = map.getCenter();
    google.maps.event.trigger(map, "resize");
    map.setCenter(center);

});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-24 17:14:02

尝试添加对position:fixed属性重要的!。如果这不起作用,那么添加jquery将js文件底部的位置设置为map.panBy(0,200)后面和map.setCenter(center);之后的固定位置。

代码语言:javascript
复制
$("#map").css("position","fixed !important");

希望这能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2019-01-19 14:27:34

在我的例子中,在使用Google和使用jQuery显示地图时也发生了同样的情况。在我的例子中,我使用CSS进行了如下修正:

代码语言:javascript
复制
#map
{
    position: fixed !important; 
    height: 100% !important;
    width: 100% !important;
}

显示地图的div元素如下:

代码语言:javascript
复制
<div id="map"></div>
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40223751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档