因此,我创建了我自己的解决方案,我将分享它以使任何具有相同问题的人受益。
我修改了yogiben/meteor-autoform-map并添加了pull request加入geo编码功能以及许多其他的东西。
以下是我目前的自动窗体配置&处理: -
模式
Location = new SimpleSchema({
location: {
type: Object,
autoform:{
type: 'map',
label: false,
afFieldInput: {
mapType: 'terrain',
defaultLat: 30.0444196,
defaultLng: 31.23571160000006,
geolocation: true,
autolocate: true,
searchBox: true,
language: function(){ return getUserLanguage(); },
direction: function(){ return (getUserLanguage() == 'ar')? 'rtl' : 'ltr'; },
zoom: 16,
key: Meteor.settings.public.google_maps_key,
googleMap: {
mapTypeControl: false
},
geoCoding: true,
geoCodingCallBack: 'reverseGeoCode',
animateMarker: true
}
}
},
placeId: {
type: String,
autoform: {
type: 'hidden'
},
autoValue: function(){
return '';
}
},
createdAt: {
type: Date,
autoValue: function(){
return new Date();
},
autoform: {
type: 'hidden'
}
}
});
Address = new SimpleSchema({
location: {
type: Location,
label: function(){
return (Session.get('lang') == 'ar')? 'الموقع على الخريطة' : 'Map Location';
}
},
country: {
type: String,
label: function(){
return (Session.get('lang') == 'ar')? 'الدولة' : 'Country';
},
autoform: {
afFieldInput: {
placeholder: function(){
return (Session.get('lang') == 'ar')? 'الدولة' : 'Country';
}
},
type: 'hidden'
}
},
governorate: {
type: String,
label: function(){
return (Session.get('lang') == 'ar')? 'المحافطة' : 'Governorate';
},
autoform: {
afFieldInput: {
placeholder: function(){
return (Session.get('lang') == 'ar')? 'المحافظة' : 'Governorate';
}
}
}
},
city: {
type: String,
label: function(){
return (Session.get('lang') == 'ar')? 'المدينة' : 'City';
},
autoform: {
afFieldInput: {
placeholder: function(){
return (Session.get('lang') == 'ar')? 'المدينة' : 'City';
}
}
}
},
district: {
type: String,
label: function(){
return (Session.get('lang') == 'ar')? 'الحي' : 'District';
},
autoform: {
afFieldInput: {
placeholder: function(){
return (Session.get('lang') == 'ar')? 'الحي' : 'District';
}
}
}
},
street: {
type: String,
label: function(){
return (Session.get('lang') == 'ar')? 'الشارع' : 'Street';
},
autoform: {
afFieldInput: {
placeholder: function(){
return (Session.get('lang') == 'ar')? 'اسم \ رقم الشارع' : 'Street Name/Number';
}
}
}
},
building: {
type: String,
label: function(){
return (Session.get('lang') == 'ar')? 'رقم المبنى' : 'Building Number';
},
regEx: /[\d+\-]/,
autoform: {
afFieldInput: {
placeholder: function(){
return (Session.get('lang') == 'ar')? 'رقم المبنى' : 'Building Number';
}
},
}
},
createdAt: {
type: Date,
autoValue: function(){
return new Date();
},
autoform: {
type: 'hidden'
}
}
});
Branch = new SimpleSchema({
name: {
type: String,
label: function(){
return (Session.get('lang') == 'ar')? 'اسم الفرع' : 'Branch Name';
},
autoform: {
afFieldInput: {
placeholder: function(){
return (Session.get('lang') == 'ar')? 'اسم الفرع' : 'Branch Name';
}
}
}
},
address: {
type: Address,
label: function(){
return (Session.get('lang') == 'ar')? 'العنوان' : 'Address';
},
minCount: 1,
optional: false
},
createdAt: {
type: Date,
autoValue: function(){
return new Date();
},
autoform: {
type: 'hidden'
}
}
});
CompaniesSchema = new SimpleSchema({
name: {
type: String,
label: function(){
return (Session.get('lang') == 'ar')? 'اسم الشركة' : 'Company Name';
},
autoform: {
afFieldInput: {
placeholder: function(){
return (Session.get('lang') == 'ar')? 'اسم الشركة' : 'Company Name';
}
}
}
},
branches: {
type: [Branch],
label: function(){
return (Session.get('lang') == 'ar')? 'الفرع' : 'Branch';
}
},
createdAt: {
type: Date,
autoValue: function(){
return new Date();
},
autoform: {
type: 'hidden'
}
}
});
客户端/ main.js
Meteor.startup(() => {
// ================================================================ //
// Functions that need to be global perior to Packages loading //
// ================================================================ //
// Get Initial/Current user language
getUserLanguage = function() {
return Session.get('lang') || Meteor.settings.public.defaultLang;
};
// Reverse Geocode location
// @param t => Template Instance - object
// @param geocoder => google.map.geocoder - object
// @param location => google.maps.LatLng - object
reverseGeoCode = function(t, geocoder, location){
var latlng = {lat: location.lat(), lng: location.lng()};
var geoCodingOpt = {
'location' : latlng,
'language' : Session.get('lang'),
'region' : 'eg'
};
geocoder.geocode(geoCodingOpt, function(results, status){
if(status === 'OK'){
if(results.length !== 0){
var address = results[0];
var cmp = address.address_components;
var pfx = t.data.name.replace(/.location/g,'');
var sel = '[name="' + pfx + '.' + 'xx' + '"]';
var selObj = {
placeId : sel.replace('xx', 'location.placeId'),
country : sel.replace('xx', 'country'),
governorate : sel.replace('xx', 'governorate'),
city : sel.replace('xx', 'city'),
district : sel.replace('xx', 'district'),
street : sel.replace('xx', 'street'),
building : sel.replace('xx', 'building')
};
// Address Container DOM element
$addressElm = $(t.firstNode.closest('.autoform-array-item'));
// Place ID
$addressElm.find(selObj.placeId).val(address.place_id);
// Country
$addressElm.find(selObj.country).val(cmp[5].long_name);
// Governorate
$addressElm.find(selObj.governorate).val(cmp[4].long_name);
// City
$addressElm.find(selObj.city).val(cmp[3].long_name);
// District
$addressElm.find(selObj.district).val(cmp[2].long_name);
// Street
$addressElm.find(selObj.street).val(cmp[1].long_name);
// Building
$addressElm.find(selObj.building).val(cmp[0].long_name);
// Clear DOM refference for Garbage Collection
$addressElm.prevObject = null;
$addressElm = null;
}
}
});
}
});
希望帮助任何人。
随时查询任何精化