我正在尝试修改jQuery Addresspicker 小部件,以便除了 Google Maps Geocoder 结果之外,它还可以从自定义数据数组返回自动完成结果。目标是让用户能够搜索商店名称,然后将其放在地图的中心,或者街道/城市/地区名称,将在未修改的小部件中显示。
问题是,我无法让小部件将自定义数据保存为其本地/方法变量。我的猜测是它与闭包有关,我已经四处寻找解决方案,但到目前为止我仍然一无所知。
自定义数据数组在小部件初始化期间设置:
var marker = createCustomDataArray();
var addresspicker = $( "#searchbox" ).addresspicker({
mapOptions: {
zoom: 17,
regionBias: "id",
center: pos,
scrollwheel: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
componentRestrictions: { country: "id" },
types: ['geocode']
},
elements: {
map: "#mapholder",
lat: "#lat",
lng: "#lng",
locality: '#locality',
country: '#country'
},
customData: marker
})
这是修改后的脚本:
$.widget( "ui.addresspicker", {
options: {
appendAddressString: "",
draggableMarker: true,
regionBias: "id",
mapOptions: {
zoom: 12,
center: new google.maps.LatLng(-5, 106.84517200000005),
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
elements: {
map: false,
lat: false,
lng: false,
locality: false,
country: false,
type: false
},
customData: null
},
setCustomData: function(data) {
this.customData = data;
return 42;
},
_create: function() {
this.geocoder = new google.maps.Geocoder();
this.element.autocomplete({
source: $.proxy(this._geocode, this),
focus: $.proxy(this._focusAddress, this),
select: $.proxy(this._selectAddress, this)
});
this.lat = $(this.options.elements.lat);
this.lng = $(this.options.elements.lng);
this.locality = $(this.options.elements.locality);
this.country = $(this.options.elements.country);
this.type = $(this.options.elements.type);
if (this.options.elements.map) {
this.mapElement = $(this.options.elements.map);
this._initMap();
}
this.setCustomData(this.options.customData);
},
有任何想法吗?