javascript - 谷歌地图 v3 像 Pinterest map (移动中心位置和改变标记区域)

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

我想通过 Google map v3 获得与 Pinterest map (例如 www.pinterest.com/airbnb/loved-by-parisians/)相同的效果。

到目前为止我已经做到了:http://jsfiddle.net/tdff3/9xEEG/

我缺少一件事: 移动中心 map 位置并将标记可见区域更改为右侧,以响应不同的分辨率。

现在:

我想要的:

function initialize()
{
    var mapOptions =
    {
        zoom: 8,
        center: new google.maps.LatLng( -33.9, 151.2 ),
        disableDefaultUI: true,
        zoomControl: true,
        zoomControlOptions:
        {
            style: google.maps.ZoomControlStyle.SMALL,
            position: google.maps.ControlPosition.RIGHT_BOTTOM
        },
        scrollwheel: false
    };

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

    var beaches =
    [
        [ 'Bondi Beach', -33.890542, 151.274856, 4 ],
        [ 'Coogee Beach', -33.423036, 151.259052, 5 ],
        [ 'Cronulla Beach', -34.028249, 121.157507, 3 ],
        [ 'Manly Beach', -33.80010128657071, 151.28747820854187, 2 ],
        [ 'Maroubra Beach', -33.450198, 151.259302, 1 ]
    ];

    setMarkers( map, beaches );
}

function setMarkers( map, locations )
{
    var bounds = new google.maps.LatLngBounds();

    for( var i = 0; i < locations.length; i++ )
    {
        var beach = locations[ i ];
        var myLatLng = new google.maps.LatLng( beach[ 1 ], beach[ 2 ] );
        var marker = new google.maps.Marker(
        {
            position: myLatLng,
            map: map,
            title: beach[ 0 ],
            zIndex: beach[ 3 ]
        } );

        bounds.extend( myLatLng );
    }

    map.fitBounds( bounds );
}

function loadScript()
{
    var script = document.createElement( 'script' );
    script.type = 'text/javascript';
    script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&' +
        'callback=initialize';
    document.body.appendChild( script );
}

window.onload = loadScript;

最佳答案

标记区域,我认为这将是所有标记的扩展边界,在您发布的 2 张图片上并不相同。

如果你在整个澳大利亚都有标记,那么边界看起来就像你的第二张照片中的那样。如果不是,但您仍然想要实现此特定 View ,那么您可能需要调整缩放级别以确保它以这种方式显示,或者手动定义这些边界。

关于 map 向右移动,可以用 map panBy() method这允许您按给定的像素距离更改 map 的中心。

希望这对您有所帮助!

关于javascript - 谷歌地图 v3 像 Pinterest map (移动中心位置和改变标记区域),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21453329/

相关文章:

html - Bootstrap 响应图像不会居中

css - 使用尽可能多的 unicode 字符获取 Google 网络字体作为 ttf?

css - 纯 CSS Dropline 菜单 - 二级菜单项位于其父项下方 - 但有时会扩展到屏幕外并添加水平滚动条

javascript - 标记内容(信息窗口)谷歌地图

javascript - 如何使用react-google-maps为使用create-react-app创建的React应用程序添加Google Maps API key ?

javascript - 如何将 URL 参数转换为 JavaScript 对象?

javascript - 打破 iframe 中的父级、顶级变量以防止框架破坏,但仍然访问父级的功能

ios - 添加和删​​除标记时重新加载 Google map View

javascript - document.getElementById 不工作,即使我的 css 中存在 ID

javascript - 我希望它继续到 javascript 测验中数组中的下一个元素