s'assurer de l'existence de jQuery

main
Paul Schneider 9 years ago
parent 1c05538b75
commit e9093acc8a
1 changed files with 88 additions and 85 deletions

@ -1,88 +1,91 @@
+(function($,maps){ if (typeof jQuery === 'undefined') {
$.widget("psc.googlegeocode" , { throw new Error('Bootstrap\'s JavaScript requires jQuery')
options: { }
mapId: 'map',
longId: 'Longitude',
latId: 'Latitude',
addrValidationId: 'AddressError',
formValidId: 'ValidationSummary',
locComboId: 'LocationCombo'
},
marker: null,
gmap: null,
_create: function() { +
this.element.addClass("googlegeocode"); (function($, maps) {
this.gmap = new maps.Map(document.getElementById(this.options.mapId), { $.widget("psc.googlegeocode", {
zoom: 16, options: {
center: { lat: 48.862854, lng: 2.2056466 } mapId: 'map',
}); longId: 'Longitude',
var _this =this; latId: 'Latitude',
this.element.rules("add", addrValidationId: 'AddressError',
{ formValidId: 'ValidationSummary',
remote: { locComboId: 'LocationCombo'
url: 'https://maps.googleapis.com/maps/api/geocode/json', },
type: 'get', marker: null,
data: { gmap: null,
sensor: false,
address: function () { return _this.element.val() } _create: function() {
}, this.element.addClass("googlegeocode");
dataType: 'json', this.gmap = new maps.Map(document.getElementById(this.options.mapId), {
dataFilter: function(datastr) { zoom: 16,
$('#'+_this.options.locComboId).html(""); center: { lat: 48.862854, lng: 2.2056466 }
var data = JSON.parse(datastr); });
data.results.forEach(function(item) { var _this = this;
if (item.formatted_address !== _this.element.val()) { this.element.rules("add", {
$('<li>'+item.formatted_address+'</li>') remote: {
.data("geoloc",item) url: 'https://maps.googleapis.com/maps/api/geocode/json',
.click(function() { _this.chooseLoc('user',item) }) type: 'get',
.css('cursor','pointer') data: {
.appendTo($('#'+_this.options.locComboId));} sensor: false,
else { } address: function() {  return _this.element.val() }
}); },
if ((data.status === 'OK') && (data.results.length == 1)) dataType: 'json',
{ dataFilter: function(datastr) {
// _this.chooseLoc('google',data.results[0]); $('#' + _this.options.locComboId).html("");
return true; var data = JSON.parse(datastr);
} data.results.forEach(function(item) {
return false; if (item.formatted_address !== _this.element.val()) {
}, $('<li>' + item.formatted_address + '</li>')
error: function() .data("geoloc", item)
{ .click(function() { _this.chooseLoc('user', item) })
// xhr, textStatus, errorThrown console.log('ajax loading error ... '+textStatus+' ... '+ errorThrown); .css('cursor', 'pointer')
return false; .appendTo($('#' + _this.options.locComboId));
} else {}
});
if ((data.status === 'OK') && (data.results.length == 1)) {
// _this.chooseLoc('google',data.results[0]);
return true;
}
return false;
},
error: function() {
// xhr, textStatus, errorThrown console.log('ajax loading error ... '+textStatus+' ... '+ errorThrown);
return false;
}
} }
} })
})}, },
chooseLoc: function(sender,loc) { chooseLoc: function(sender, loc) {
if (sender === 'user') this.element.val(loc.formatted_address); if (sender === 'user') this.element.val(loc.formatted_address);
var pos = loc.geometry.location; var pos = loc.geometry.location;
var lat = new Number(pos.lat); var lat = new Number(pos.lat);
var lng = new Number(pos.lng); var lng = new Number(pos.lng);
$(document.getElementById(this.options.latId)).val(lat.toLocaleString('en')); $(document.getElementById(this.options.latId)).val(lat.toLocaleString('en'));
$(document.getElementById(this.options.longId)).val(lng.toLocaleString('en')); $(document.getElementById(this.options.longId)).val(lng.toLocaleString('en'));
this.gmap.setCenter(pos); this.gmap.setCenter(pos);
if (this.marker) {  if (this.marker) { 
this.marker.setMap(null); this.marker.setMap(null);
} }
this.marker = new maps.Marker({ this.marker = new maps.Marker({
map: this.gmap, map: this.gmap,
draggable: true, draggable: true,
animation: maps.Animation.DROP, animation: maps.Animation.DROP,
position: pos position: pos
}); });
maps.event.addListener(this.marker, 'dragend', function() { maps.event.addListener(this.marker, 'dragend', function() {
// TODO reverse geo code // TODO reverse geo code
var pos = this.marker.getPosition(); var pos = this.marker.getPosition();
$('#'+this.options.latId).val(pos.lat); $('#' + this.options.latId).val(pos.lat);
$('#'+this.options.longId).val(pos.lng); $('#' + this.options.longId).val(pos.lng);
}); });
this.element.valid(); this.element.valid();
$('#'+this.options.addrValidationId).empty(); $('#' + this.options.addrValidationId).empty();
$('#'+this.options.formValidId).empty(); $('#' + this.options.formValidId).empty();
$('#'+this.options.locComboId).empty(); $('#' + this.options.locComboId).empty();
return this; return this;
} }
}) })
})(jQuery,google.maps) })(jQuery, google.maps);
Loading…