在我的javascript';类别';
我正在尝试编写一些JavaScript来处理GoogleMaps的一系列函数,但是可以在一个页面上对多个地图进行重用 我遇到的问题是,函数中调用的函数似乎忽略了javascript“类”顶部声明的变量(我知道它实际上不是一个类) geocodeAddress函数调用Googles geocoder.geocode API函数,该函数接受要调用的函数,并将结果作为参数。在此结果函数中,我无权访问“类”中的其他属性,所有属性都设置为“未定义”。我也不能调用任何其他函数 有人有什么想法吗?这是可能的,还是我应该放弃这种风格,只需将map对象从一个方法传递到另一个方法,就可以与其他映射重用在我的javascript';类别';,javascript,google-maps,maps,Javascript,Google Maps,Maps,我正在尝试编写一些JavaScript来处理GoogleMaps的一系列函数,但是可以在一个页面上对多个地图进行重用 我遇到的问题是,函数中调用的函数似乎忽略了javascript“类”顶部声明的变量(我知道它实际上不是一个类) geocodeAddress函数调用Googles geocoder.geocode API函数,该函数接受要调用的函数,并将结果作为参数。在此结果函数中,我无权访问“类”中的其他属性,所有属性都设置为“未定义”。我也不能调用任何其他函数 有人有什么想法吗?这是可能的,
function GoogleMap(settings) {
var map;
this.zoom = settings.zoom;
this.center = new google.maps.LatLng(settings.lat, settings.lng);
this.mapContainerId = settings.mapContainerId;
this.initializeGoogleMap = function initializeGoogleMap(mapOptions) {
this.map = new google.maps.Map(document.getElementById(this.mapContainerId), { zoom: this.zoom, center: this.center });
}
this.addMapMarker = function addMapMarker(markerOptions) {
// add a marker here
}
this.geocodeAddress = function geocodeAddress(location) {
// I have full access to this.zoom, this.center etc. here
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': location }, function (results, status) {
// this.zoom, this.center etc. are inaccessible here and return undefined.
if (status == google.maps.GeocoderStatus.OK) {
this.map.setCenter(results[0].geometry.location);
this.addMapMarker({ center: results[0].geometry.location, draggable: true, title: location });
} else {
alert('Could not find the address entered');
}
});
}
google.maps.event.addDomListener(window, 'load', this.initializeGoogleMap());
})
谢谢回调不会在本机上为您保留
此
的值,因此您需要做一些事情来设置它。您可以创建一个闭包变量,也可以对回调使用.bind()
这里有一个使用闭包变量self的解决方案:
this.geocodeAddress = function geocodeAddress(location) {
var self = this;
// I have full access to this.zoom, this.center etc. here
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': location }, function (results, status) {
// this.zoom, this.center etc. are inaccessible here and return undefined.
if (status == google.maps.GeocoderStatus.OK) {
self.map.setCenter(results[0].geometry.location);
self.addMapMarker({ center: results[0].geometry.location, draggable: true, title: location });
} else {
alert('Could not find the address entered');
}
});
}
使用.bind()
的解决方案:
看起来你对
这个的工作原理做了一些无效的假设。啊,这么简单的解决方案!谢谢它也适用于我添加var self=this;在最顶端,就在“GoogleMap”函数声明之后。@JGDEV-是的,这是有效的,因为您的所有方法都是在构造函数中声明的,所以它们都可以访问构造函数中的局部变量。如果你使用原型来实现你的方法,那是行不通的。在方法中声明它以任何方式工作。
this.geocodeAddress = function geocodeAddress(location) {
// I have full access to this.zoom, this.center etc. here
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': location }, function (results, status) {
// this.zoom, this.center etc. are inaccessible here and return undefined.
if (status == google.maps.GeocoderStatus.OK) {
this.map.setCenter(results[0].geometry.location);
this.addMapMarker({ center: results[0].geometry.location, draggable: true, title: location });
} else {
alert('Could not find the address entered');
}
}.bind(this));
}