【问题标题】:The map is not center all the time地图始终不在中心
【发布时间】:2012-03-30 18:30:11
【问题描述】:

我使用以下代码将地图居中,因此将标记置于中心。问题是标记大部分时间都在左上角,当我放大和缩小时,标记将再次位于左上角。

怎么了?您可以通过erik-edgren.nu/weather 实时查看问题/站点。

function initialize_map() {
    var myOptions = {
        zoom: 4,
        mapTypeControl: true,
        navigationControl: true,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        },
        streetViewControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
}

function initialize() {
    $('#map_canvas').hide();

    if(geo_position_js.init()) {
        $('#current').html('Söker efter din plats. Var god vänta...');
        geo_position_js.getCurrentPosition(show_position, function() {
            $('#current').html('Kunde tyvärr inte hitta din position.');
        }, {
            enableHighAccuracy: true
        });
    } else {
        $('#current').html('Det verkar som att din webbläsare inte tillåter att webbsidan hämtar din position. Ta en titt i webbläsarens inställningar för att se om denna funktion är igång eller inte, och försök sedan igen.');
    }
}

function show_position(p) {
    $('#current').html('');
    $('#map_canvas').show();

    var pos = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
    map.setCenter(pos);
    map.setZoom(10);

    var marker = new google.maps.Marker({
        position: pos,
        map: map
    });

    $('#weather-data').load('weather-set.php?lat=' + p.coords.latitude.toFixed(6).replace(/\./, '') + '&long=' + p.coords.longitude.toFixed(6).replace(/\./, ''));
}

提前致谢!

【问题讨论】:

    标签: google-maps google-maps-markers center


    【解决方案1】:

    确保您在 map_canvas div 元素上明确设置了大小,我在某些浏览器上遇到了这个问题,但我不小心把它遗漏了。

    <div id="map_canvas" style="width: 100%; height: 100%"></div> 
    

    来自API docs

    请注意,地图的大小总是取自它的大小 包含元素,因此您必须始终在该元素上设置大小 明确的。

    【讨论】:

    • 啊。好的。所以我必须在宽度和高度上都使用 100%?
    • 我认为不一定需要 100%,我认为任何值都可以。它甚至可以是 px 而不是 %。
    • 好的。我的地图高度为 150 像素,宽度为 100%,但我遇到了这个问题:/
    • 我在他们的 API 页面上进一步阅读了您链接的内容,发现坐标最多包含 6 位带点的数字,也可能包含减号(例如 -34.397、150.644)。我使用的坐标长度超过 9 位!我希望这可以解决问题,我目前正在对页面进行错误测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多