Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript设置器函数_Javascript_Google Maps - Fatal编程技术网

Javascript设置器函数

Javascript设置器函数,javascript,google-maps,Javascript,Google Maps,我正在js中创建一个类函数,如下所示,用于创建新的Google地图 我有一个问题,有一个默认值的缩放变量,并能够改变它后,对象已经创建 下面是函数的一段代码: function GMap(lat,lng){ this.latitude = lat; this.longitude = lng; this.mapCenter = new google.maps.LatLng(lat, lng); this.map = null; this.zoom = 20;

我正在js中创建一个类函数,如下所示,用于创建新的Google地图

我有一个问题,有一个默认值的缩放变量,并能够改变它后,对象已经创建

下面是函数的一段代码:

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

您可以使用函数设置或获取地图的属性。 我认为使用此函数可以修改缩放属性。 我希望这对你有用


什么是
GoogleMap
?需要时按当前值动态创建
this.mapOptions
,在当前代码中,该值在创建时设置,设置
this.zoom
不会更改其值。因此,无论您将
this.mapOptions.zoom
设置为
9
,都将始终为
20
。您将立即执行
gMap.createMap('mapDiv')
,结果作为回调传递。回调应该是一个函数:
…addDomListener(窗口,'load',函数(){gMap.createMap('mapDiv')})非常感谢@fuyushimoya您的回答帮助我解决了问题!