238 lines
6.1 KiB
JavaScript
238 lines
6.1 KiB
JavaScript
|
var map = null;
|
||
|
var geocoder = null;
|
||
|
var nbAddressesProcessed = 0;
|
||
|
var requestDelay = 500;
|
||
|
var maxSliceSize = 20;
|
||
|
var slice = [];
|
||
|
var distanceNA = 'N/A';
|
||
|
var isStopped = false;
|
||
|
var statusDiv = null;
|
||
|
var markers = [];
|
||
|
var polylines = [];
|
||
|
|
||
|
var LOCATIONS = {
|
||
|
|
||
|
index: -1,
|
||
|
elements: [],
|
||
|
|
||
|
processNext : function(noIncrement){
|
||
|
if(typeof noIncrement=='undefined'){
|
||
|
this.index++;
|
||
|
}
|
||
|
if(this.elements.length && this.index < this.elements.length){
|
||
|
this.processCallback(this);
|
||
|
}else{
|
||
|
this.endCallback(this);
|
||
|
}
|
||
|
},
|
||
|
|
||
|
set: function(property,value){
|
||
|
this.elements[this.index][property] = value;
|
||
|
},
|
||
|
|
||
|
get : function(){
|
||
|
return this.elements[this.index];
|
||
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
function initGoogle(){
|
||
|
var elem = document.getElementById('map');
|
||
|
var options = {
|
||
|
zoom: 2,
|
||
|
center: new google.maps.LatLng(51.500152,-0.126236),
|
||
|
mapTypeId: google.maps.MapTypeId.ROADMAP
|
||
|
};
|
||
|
map = new google.maps.Map(elem, options);
|
||
|
geocoder = new google.maps.Geocoder();
|
||
|
statusDiv = document.getElementById('search-status').getElementsByTagName('div')[0];
|
||
|
}
|
||
|
|
||
|
function geolocateLocation1(callback){
|
||
|
|
||
|
if(LOCATION1.id){
|
||
|
LOCATION1.position = new google.maps.LatLng(LOCATION1.lat, LOCATION1.lng);
|
||
|
callback();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
geocoder.geocode({'address': LOCATION1.adr}, function(results, status){
|
||
|
|
||
|
if(status != google.maps.GeocoderStatus.OK){
|
||
|
alert('The address ['+LOCATION1.adr+'] has caused an error.\nGoogle Geocoder Status: '+status);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var position = results[0].geometry.location;
|
||
|
LOCATION1.lat = position.lat();
|
||
|
LOCATION1.lng = position.lng();
|
||
|
|
||
|
jQuery.post('save-location1.php', {location: LOCATION1}, function(locationId){
|
||
|
|
||
|
if(locationId){
|
||
|
LOCATION1.id = locationId;
|
||
|
LOCATION1.position = position;
|
||
|
callback();
|
||
|
}else{
|
||
|
alert('The address ['+LOCATION1.adr+'] has caused an error.\nThe application could not save the location. '+locationId);
|
||
|
}
|
||
|
|
||
|
});
|
||
|
|
||
|
});
|
||
|
|
||
|
}
|
||
|
|
||
|
function geolocateLocationsAll(){
|
||
|
jQuery.get('get-locations.php?geosrc='+geosrc+'&locationId='+LOCATION1.id, function(json){
|
||
|
jQuery('#btnStop').show();
|
||
|
LOCATIONS.processCallback = geolocateLocationN;
|
||
|
LOCATIONS.endCallback = geolocateLocationsEnd;
|
||
|
LOCATIONS.elements = eval(json);
|
||
|
LOCATIONS.processNext();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function saveLocationOnServer(location, delay) {
|
||
|
nbAddressesProcessed++;
|
||
|
showDistance(location);
|
||
|
slice.push(location);
|
||
|
if(slice.length>=maxSliceSize){
|
||
|
saveSlice();
|
||
|
setTimeout(clearMarkers, 500);
|
||
|
setTimeout(function(){ LOCATIONS.processNext(); }, 1000);
|
||
|
}else{
|
||
|
setTimeout(function(){ LOCATIONS.processNext(); }, delay);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function geolocateLocationN(){
|
||
|
|
||
|
if(isStopped){
|
||
|
doAbort();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var location = LOCATIONS.get();
|
||
|
|
||
|
if(location.dst){
|
||
|
saveLocationOnServer(location, 0);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if(location.id){
|
||
|
var position = new google.maps.LatLng(location.lat, location.lng);
|
||
|
location.dst = google.maps.geometry.spherical.computeDistanceBetween(LOCATION1.position, position);
|
||
|
saveLocationOnServer(location, 0);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
geocoder.geocode({'address': location.adr}, function(results, status){
|
||
|
|
||
|
if(status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT){
|
||
|
setTimeout(function(){
|
||
|
LOCATIONS.processNext(false);
|
||
|
}, 5000);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if(status != google.maps.GeocoderStatus.OK){
|
||
|
$('#map').css('opacity','0.3');
|
||
|
$('#options').show(666);
|
||
|
$('#options h3 span').html(location.adr);
|
||
|
$('#options input').val(location.adr);
|
||
|
jQuery('#btnStop').hide();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var position = results[0].geometry.location;
|
||
|
location.lat = position.lat();
|
||
|
location.lng = position.lng();
|
||
|
location.dst = google.maps.geometry.spherical.computeDistanceBetween(LOCATION1.position, position);
|
||
|
|
||
|
saveLocationOnServer(location, requestDelay);
|
||
|
|
||
|
});
|
||
|
|
||
|
}
|
||
|
|
||
|
function geolocateLocationsEnd(){
|
||
|
setTimeout(saveSlice, 1000);
|
||
|
jQuery('#search-complete').show();
|
||
|
jQuery('#btnStop').hide();
|
||
|
}
|
||
|
|
||
|
function showDistance(location){
|
||
|
var pos1 = LOCATION1.position;
|
||
|
var pos2 = new google.maps.LatLng(location.lat, location.lng);
|
||
|
var marker2 = new google.maps.Marker({position:pos2});
|
||
|
var polyline = new google.maps.Polyline({path: [pos1,pos2], strokeColor: "#FF0000", strokeOpacity: 1.0,strokeWeight: 1});
|
||
|
marker2.setMap(map);
|
||
|
polyline.setMap(map);
|
||
|
showStatus(location.adr, location.dst);
|
||
|
if(!markers.length){
|
||
|
var marker1 = new google.maps.Marker({position:pos1});
|
||
|
marker1.setMap(map);
|
||
|
markers.push(marker1);
|
||
|
}
|
||
|
markers.push(marker2);
|
||
|
polylines.push(polyline);
|
||
|
}
|
||
|
|
||
|
function saveSlice(){
|
||
|
if(slice.length){
|
||
|
jQuery.post('save-locations.php?locationId='+LOCATION1.id, {locations:slice});
|
||
|
slice = [];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function doResume() {
|
||
|
unhideMap();
|
||
|
setTimeout(function(){
|
||
|
LOCATIONS.set('adr', $('#options input').val());
|
||
|
LOCATIONS.processNext(false);
|
||
|
},1000);
|
||
|
}
|
||
|
|
||
|
function doSkip() {
|
||
|
nbAddressesProcessed++;
|
||
|
unhideMap();
|
||
|
showStatus($('#options input').val(), distanceNA);
|
||
|
LOCATIONS.set('dst', distanceNA);
|
||
|
slice.push(LOCATIONS.get());
|
||
|
LOCATIONS.processNext();
|
||
|
}
|
||
|
|
||
|
function doAbort() {
|
||
|
unhideMap();
|
||
|
showStatus($('#options input').val(), distanceNA);
|
||
|
geolocateLocationsEnd();
|
||
|
}
|
||
|
|
||
|
function showStatus(address, distance) {
|
||
|
if(distance!=distanceNA){
|
||
|
distance = Math.ceil(distance/1000);
|
||
|
}
|
||
|
var str = 'Addresses processed: '+nbAddressesProcessed+' / '+LOCATIONS.elements.length;
|
||
|
str += '<br>Last: '+distance+' km to '+address;
|
||
|
statusDiv.innerHTML = str;
|
||
|
}
|
||
|
|
||
|
function unhideMap() {
|
||
|
$('#map').css('opacity','1');
|
||
|
$('#options').hide(500);
|
||
|
jQuery('#btnStop').show();
|
||
|
}
|
||
|
|
||
|
function clearMarkers() {
|
||
|
for (var i = 0; i < markers.length; i++) {
|
||
|
markers[i].setMap(null);
|
||
|
}
|
||
|
for (var i = 0; i < polylines.length; i++) {
|
||
|
polylines[i].setMap(null);
|
||
|
}
|
||
|
polylines = [];
|
||
|
markers = [];
|
||
|
}
|
||
|
|