Javascript 放大以标记 google.maps

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5054515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:26:40  来源:igfitidea点击:

Zoom in to marker google.maps

javascriptgoogle-mapsgoogle-maps-api-3

提问by Jakob

I can't zoom to a marker properly.

我无法正确缩放到标记。

I'm trying to switch the view to a specified marker, but can't ge tit to work.

我正在尝试将视图切换到指定的标记,但无法让奶头工作。

I've tried

我试过了

map.setCenter(location);
map.setZoom(20);

and

map.fitBounds(new google.maps.latLngBounds(location,location));

but in the first case, I simply get zoomed in without the center change being registered, and in the second case I get this overview over a huge area, not zoomed in at all.

但在第一种情况下,我只是在没有注册中心变化的情况下放大,而在第二种情况下,我在一个巨大的区域内得到了这个概览,根本没有放大。

Perhaps this issue could be solved by setting a timout from setcenter to setzoom, but that's an ugly hack to me, so a prettier solution would be preferred.

也许这个问题可以通过设置从 setcenter 到 setzoom 的时间来解决,但这对我来说是一个丑陋的黑客,所以更漂亮的解决方案是首选。

How do you guys do this?

你们是怎么做到的?

Also - if the infowindow could be displayed without changing content, that would really be a plus to, but the most important thing is to zoom into the marker at the right spot, close up.

另外 - 如果可以在不更改内容的情况下显示信息窗口,那确实是一个加分项,但最重要的是在正确的位置放大标记,关闭。

thank you very much.

非常感谢您。

回答by Jakob

The solution turned out to be

解决方案原来是

map.setZoom(17);
map.panTo(curmarker.position);

回答by AdRock

I thought I would post an answer here as people wanted some example code.

我想我会在这里发布一个答案,因为人们想要一些示例代码。

I too needed to be able to zoom in and center as soon as a marker was added to the map.

我也需要能够在标记添加到地图后立即放大和居中。

Hope this helps somebody.

希望这可以帮助某人。

function getPoint(postcode) {

    var geocoder = new google.maps.Geocoder();

    geocoder.geocode( { 'address': postcode + ', UK'}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });

            map.setZoom(10);
            map.panTo(marker.position);
        }
        else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}

回答by Will Ayd

is this a new instance of a map object you are creating? if so you can just have an object which contains the location and zoom and then pass that object to the map when initalising it like so (taken from the Gmaps basics tutorial http://code.google.com/apis/maps/documentation/javascript/basics.html:

这是您正在创建的地图对象的新实例吗?如果是这样,你可以只拥有一个包含位置和缩放的对象,然后在初始化时将该对象传递给地图(取自 Gmaps 基础教程http://code.google.com/apis/maps/documentation/ javascript/basics.html

function initialize() {
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}