Javascript 使用Prototypejs的Google maps API V3方法fitBounds()
我有一个div,用于显示谷歌地图,如下所示:Javascript 使用Prototypejs的Google maps API V3方法fitBounds(),javascript,jquery,google-maps,google-maps-api-3,prototypejs,Javascript,Jquery,Google Maps,Google Maps Api 3,Prototypejs,我有一个div,用于显示谷歌地图,如下所示: #map { width: 300px; height: 300px; border: 1px solid #DDD; } <div id="map"></div> 当我使用生成地图时,它放大得太远了吗 我已经把它缩小到以下代码。调用AddressPicker.prototype.updateMap函数时,AddressPicker.prototype.initMap函数上的boundsForLocati
#map {
width: 300px;
height: 300px;
border: 1px solid #DDD;
}
<div id="map"></div>
当我使用生成地图时,它放大得太远了吗
我已经把它缩小到以下代码。调用AddressPicker.prototype.updateMap
函数时,AddressPicker.prototype.initMap
函数上的boundsForLocation
选项应返回this.map.fitbunds(response.geometry.viewport)代码>调试时,我可以看到它按预期在AddressPicker.prototype.updateBoundsForPlace
函数中运行以下代码:
if (response.geometry.viewport) {
console.log('test');
return this.map.fitBounds(response.geometry.viewport);
}
我不明白的是它是如何连接回google.maps.Map的——我不熟悉ptotypeJS?基本上,我们通过调用initMap初始化映射,然后调用updateMap函数。在updateMap函数中,我们调用以下代码片段:
if (_this.map) {
if ((_ref = _this.mapOptions) != null) {
_ref.boundsForLocation(response);
}
}
假设哪一个通过调用updateBoundsForPlace
来设置边界,但不公开任何名为boundsForLocation的属性
AddressPicker.prototype.initMap = function() {
var markerOptions, _ref, _ref1;
if ((_ref = this.options) != null ? (_ref1 = _ref.map) != null ? _ref1.gmap : void 0 : void 0) {
this.map = this.options.map.gmap;
} else {
this.mapOptions = $.extend({
zoom: 3,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP,
boundsForLocation: this.updateBoundsForPlace
}, this.options.map);
this.map = new google.maps.Map($(this.mapOptions.id)[0], this.mapOptions);
}
this.lastResult = null;
markerOptions = $.extend({
draggable: true,
visible: false,
position: this.map.getCenter(),
map: this.map
}, this.options.marker || {});
this.marker = new google.maps.Marker(markerOptions);
if (markerOptions.draggable) {
return google.maps.event.addListener(this.marker, 'dragend', this.markerDragged);
}
};
AddressPicker.prototype.updateMap = function(event, place) {
if (this.options.placeDetails) {
return this.placeService.getDetails(place, (function(_this) {
return function(response) {
var _ref;
_this.lastResult = new AddressPickerResult(response);
if (_this.marker) {
_this.marker.setPosition(response.geometry.location);
_this.marker.setVisible(true);
}
if (_this.map) {
if ((_ref = _this.mapOptions) != null) {
_ref.boundsForLocation(response);
}
}
return $(_this).trigger('addresspicker:selected', _this.lastResult);
};
})(this));
} else {
return $(this).trigger('addresspicker:selected', place);
}
};
AddressPicker.prototype.updateBoundsForPlace = function(response) {
if (response.geometry.viewport) {
return this.map.fitBounds(response.geometry.viewport);
} else {
this.map.setCenter(response.geometry.location);
return this.map.setZoom(this.options.zoomForLocation);
}
};
通过注释掉以下行来修复:
//if (response.geometry.viewport) {
// return this.map.fitBounds(response.geometry.viewport);
//} else {
this.map.setCenter(response.geometry.location);
return this.map.setZoom(this.options.zoomForLocation);
//}
你有没有办法用小提琴来演示这个问题?我不太明白你想问什么。在initMap中,this.map被设置为谷歌地图:this.map=this.options.map.gmap;或者this.map=new google.maps.map($(this.mapOptions.id)[0],this.mapOptions)@Scott我理解这一点,但当您调用updateMap函数时,它需要设置boundsForLocation。但是看看api MapOptions没有一个名为boundsForLocation的属性,那么它是如何设置的呢?
//if (response.geometry.viewport) {
// return this.map.fitBounds(response.geometry.viewport);
//} else {
this.map.setCenter(response.geometry.location);
return this.map.setZoom(this.options.zoomForLocation);
//}