![]() |
干练的滑板 · 学院获批一项驻鲁部属高校“十四五”服务山东重 ...· 5 月前 · |
![]() |
儒雅的板凳 · python提取字符串中的某某大学或学院 ...· 9 月前 · |
![]() |
伤情的红豆 · 狗头萝莉不雅视频被疯传?其曝光悲惨经历,被侵 ...· 10 月前 · |
![]() |
活泼的草稿本 · 忆起过去的时光,前武汉卓尔投资人阎志发表长诗 ...· 11 月前 · |
![]() |
干练的水煮肉 · 南宁市教育局关于做好南宁市高层次人才及海外人 ...· 1 年前 · |
我正在使用fitBounds()在我的地图上设置缩放级别,包括当前显示的所有标记。但是,当我只有一个标记可见时,缩放级别是100% (...我想是哪个变焦级别20 )。但是,我不希望它被放大那么远,这样用户就可以调整标记的位置,而不必缩小。
我有以下代码:
var marker = this.map.createMarker(view.latlng, this.markerNumber);
this.map.bounds.extend(view.latlng);
this.map.map.setCenter(this.map.bounds.getCenter());
this.map.map.fitBounds(this.map.bounds);
if (this.markerNumber === 1) {
this.map.map.setZoom(16);
this.markerNumber++;
其中,this.map.bounds以前定义为:
this.map.bounds = new google.maps.LatLngBounds();
然而,我遇到的问题是,如果我使用
this.map.map.fitBounds(this.map.bounds);
,
this.map.map.setZoom(16);
行就不能工作,但是,我知道这行代码是正确的,因为当我注释掉fitBound()行时,setZoom()就神奇地开始工作了。
你知道我该怎么解决这个问题吗?如果我不能让它工作,我正在考虑设置一个maxZoom级别作为替代。
我有一个简单而肮脏的解决方案。
使用If else ...
var marker = this.map.createMarker(view.latlng, this.markerNumber);
this.map.bounds.extend(view.latlng);
this.map.map.setCenter(this.map.bounds.getCenter());
if (this.markerNumber === 1) {
this.map.map.setZoom(16);
} else {
this.map.map.fitBounds(this.map.bounds);
this.markerNumber++;
我只向函数
addBounds(position)
添加了一行代码,并将其修复,如下所示:
addBounds: function(position) {
this.get('bounds', new google.maps.LatLngBounds()).extend(this._latLng(position));
this.get('map').fitBounds(this.get('bounds'));
this.get('map').setZoom(16);//line added
return this;
},
google.maps.event.addListenerOnce(yourMap, 'bounds_changed', function(event) {
if (this.getZoom() > 15) {
this.setZoom(15);
});
这个解决方案工作得更好,…而不是等待超时来移除监听器。在使用
fitBounds
之前直接调用它(我相信之后调用也可以)。
我发现额外的变焦有点刺耳。如果您在调用fitBounds之前设置了maxZoom选项(然后在回调中取消设置),则可以避免这种情况:
map.setOptions({
maxZoom: 10
map.setCenter(new google.maps.LatLng(-89, -179)); // make sure it changes so the idle listener gets called back
map.fitBounds(bounds);
var listener = google.maps.event.addListenerOnce(map, "idle", function()
map.setOptions({
maxZoom: 999
![]() |
干练的滑板 · 学院获批一项驻鲁部属高校“十四五”服务山东重点建设项目 5 月前 |