我正在 js 中创建一个类函数,如下所示,以创建新的 Google map 。
我在缩放变量的默认值方面遇到问题,并且能够在创建对象后更改它。
这是该函数的一段代码:
function GMap(lat,lng){
this.latitude = lat;
this.longitude = lng;
this.mapCenter = new google.maps.LatLng(lat, lng);
this.map = null;
this.zoom = 20; // As a default value
this.setZoom = function(value)
{
this.zoom = value;
}
this.getZoom = function()
{
return this.zoom;
}
this.mapOptions = {
center: this.mapCenter,
zoom: this.zoom
};
this.createMap = function(htmlElement)
{
map = new google.maps.Map(document.getElementById(htmlElement), this.mapOptions);
return this.map;
}
}
我这样称呼上面的内容:
var gMap= new GMap(10, 20);
gMap.setZoom(9);
console.log(gMap.getZoom()); //Returns 9 successfully
google.maps.event.addDomListener(window, 'load', gMap.createMap('mapDiv')); //but creates a map with default zoom 20
最佳答案
您有设置或获取 map 属性的函数。 我认为使用此功能您可以修改缩放属性。 我希望这对您有用。
关于JavaScript 设置函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31829253/