yavsc/src/Yavsc/wwwroot/js/google-geoloc.js

106 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-05-28 00:44:05 +02:00
if (typeof jQuery === 'undefined') {
2019-09-01 01:31:55 +01:00
throw new Error('This Google maps client script requires jQuery')
}
if (typeof google === 'undefined') {
throw new Error('This Google maps client script requires google')
2017-05-28 00:44:05 +02:00
}
2017-04-01 02:14:10 +02:00
2017-05-28 00:44:05 +02:00
(function($, maps) {
2019-09-01 01:31:55 +01:00
$.widget('psc.googlegeocode', {
2017-05-28 00:44:05 +02:00
options: {
2019-09-01 01:31:55 +01:00
culture: 'fr',
2017-05-28 00:44:05 +02:00
mapId: 'map',
longId: 'Longitude',
latId: 'Latitude',
addrValidationId: 'AddressError',
formValidId: 'ValidationSummary',
2019-09-01 01:31:55 +01:00
locComboId: 'LocationCombo',
specifyPlaceMsg: 'Specify a place',
GoogleDidntGeoLocalizedMsg: 'Google didn\'t reconized this address'
2017-05-28 00:44:05 +02:00
},
marker: null,
gmap: null,
2019-09-01 01:31:55 +01:00
onDragEnd: function(_this) {
// TODO reverse geo code
var npos = _this.marker.getPosition();
var nlat = Number(npos.lat());
var nlng = Number(npos.lng());
$('#' + _this.options.latId).val(nlat.toLocaleString(_this.options.culture, { minimumFractionDigits: 8 }));
$('#' + _this.options.longId).val(nlng.toLocaleString(_this.options.culture, { minimumFractionDigits: 8 }));
},
2017-05-28 00:44:05 +02:00
_create: function() {
2019-09-01 01:31:55 +01:00
var _this = this;
var scenter = { lat: parseFloat($('#' + _this.options.latId).val().replace(',', '.')),
lng: parseFloat($('#' + _this.options.longId).val().replace(',', '.')) };
this.element.addClass('googlegeocode');
2017-05-28 00:44:05 +02:00
this.gmap = new maps.Map(document.getElementById(this.options.mapId), {
zoom: 16,
2019-09-01 01:31:55 +01:00
center: scenter
2017-05-28 00:44:05 +02:00
});
2019-09-01 01:31:55 +01:00
this.marker = new maps.Marker({
map: this.gmap,
draggable: true,
animation: maps.Animation.DROP,
position: scenter
});
maps.event.addListener(this.marker, 'dragend', function() { _this.onDragEnd(_this) });
this.element.data('val-required', this.options.specifyPlaceMsg);
this.element.data('val-remote', this.options.GoogleDidntGeoLocalizedMsg);
this.element.rules('add', {
2017-05-28 00:44:05 +02:00
remote: {
url: 'https://maps.googleapis.com/maps/api/geocode/json',
type: 'get',
data: {
2019-09-01 01:31:55 +01:00
key: _this.options.mapsApiKey,
2017-05-28 00:44:05 +02:00
sensor: false,
2019-09-01 01:31:55 +01:00
address: function() {
return _this.element.val();
}
2017-05-28 00:44:05 +02:00
},
dataType: 'json',
dataFilter: function(datastr) {
2019-09-01 01:31:55 +01:00
var ul = $('#' + _this.options.locComboId);
ul.html('');
2017-05-28 00:44:05 +02:00
var data = JSON.parse(datastr);
data.results.forEach(function(item) {
2019-09-01 01:31:55 +01:00
$('<li>' + item.formatted_address + '</li>')
.data('geoloc', item)
.click(function() { _this.chooseLoc('user', item) })
.css('cursor', 'pointer')
.appendTo(ul);
2017-05-28 00:44:05 +02:00
});
2019-09-01 01:31:55 +01:00
if ((data.status === 'OK') && (data.results.length >= 1)) {
2017-05-28 00:44:05 +02:00
return true;
}
return false;
}
2017-04-01 02:14:10 +02:00
}
2017-05-28 00:44:05 +02:00
})
},
chooseLoc: function(sender, loc) {
2019-09-01 01:31:55 +01:00
var _this = this;
2017-05-28 00:44:05 +02:00
if (sender === 'user') this.element.val(loc.formatted_address);
var pos = loc.geometry.location;
2019-09-01 01:31:55 +01:00
var lat = Number(pos.lat);
var lng = Number(pos.lng);
$(document.getElementById(this.options.latId)).val(lat.toLocaleString(this.options.culture, { minimumFractionDigits: 8 }));
$(document.getElementById(this.options.longId)).val(lng.toLocaleString(this.options.culture, { minimumFractionDigits: 8 }));
2017-05-28 00:44:05 +02:00
this.gmap.setCenter(pos);
2019-09-01 01:31:55 +01:00
if (this.marker) {
2017-05-28 00:44:05 +02:00
this.marker.setMap(null);
}
this.marker = new maps.Marker({
map: this.gmap,
draggable: true,
animation: maps.Animation.DROP,
position: pos
2017-04-01 02:14:10 +02:00
});
2019-09-01 01:31:55 +01:00
maps.event.addListener(this.marker, 'dragend', function() { _this.onDragEnd(_this) });
2017-05-28 00:44:05 +02:00
this.element.valid();
$('#' + this.options.addrValidationId).empty();
$('#' + this.options.formValidId).empty();
$('#' + this.options.locComboId).empty();
return this;
}
})
2019-09-01 01:31:55 +01:00
})(jQuery, google.maps);