Correction pour la carte

This commit is contained in:
Michael RICOIS 2015-03-30 09:24:26 +00:00
parent 6616e7c86a
commit 445a33f64f

View File

@ -1,68 +1,123 @@
<div id="carte" style="width:100%;height:500px"></div>
<div id="carte" style="width:100%;height:500px">Préparation de la carte ...</div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
<?php echo 'var marks = '.$this->marks.';'?>
$(document).ready(function(){
var timer;
var timerSecondInterval = 5;
var timerSecondMax = 120;
var timeCount = 0;
var marksNb = marks.length;
var markDone = 1;
var map;
var zoneMarker;
var geocoder = new google.maps.Geocoder();
var zoneMarker = new google.maps.LatLngBounds();
// --- Wait GeoCoding to launch Google Map
function waitGeoCode() {
timeCount = timeCount + timerSecondInterval;
if (timeCount >= timerSecondMax || markDone == marksNb) {
clearInterval(timer);
loadGoogleMap();
}
}
//Create the map
var geocoder = new google.maps.Geocoder();
var initCenter = new google.maps.LatLng(46.227638,2.213749);
//--- Create Marker
function createMarker(m) {
// --- Marker options
var marker = new google.maps.Marker({
map: map,
position: m.location,
title: m.title,
icon: m.icon,
});
// --- Info Window
var infowindow = new google.maps.InfoWindow({
content: m.text
});
// --- Create event marker
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
// --- Extend map by adding marker
zoneMarker.extend(marker.getPosition());
}
// --- Load Google Map
function loadGoogleMap() {
var mapOptions = {
zoom: 10,
center: initCenter,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('carte'), mapOptions);
var length = marks.length;
for (var i = 0; i < length; i++) {
if ( marks[i]['gps']['lat'] && marks[i]['gps']['lon']) {
//console.log("GPS OK " + marks[i]['address'] + ' - CACHE');
var location = new google.maps.LatLng(marks[i]['gps']['lat'],marks[i]['gps']['lon']);
createMarker(marks[i], location);
} else {
codeAddress(marks[i]);
map = new google.maps.Map(document.getElementById('carte'), mapOptions);
zoneMarker = new google.maps.LatLngBounds();
$.each(marks, function (i, item){
if (item.location) {
createMarker(item);
}
});
if (zoneMarker.isEmpty()) {
//console.log('ZoneMarker is empty');
} else {
map.fitBounds(zoneMarker);
}
}
map.fitBounds(zoneMarker);
//Bounds the maps
function codeAddress(mark) {
geocoder.geocode( {'address': mark['address'], region:'FR'}, function(results, status) {
//console.log("GPS KO " + mark['address'] + ' - ' + status);
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
//Query to set the GPS coord in our database
createMarker(mark,location);
}
});
}
//Create Marker
function createMarker(m, location){
//Create marker
var marker = new google.maps.Marker({
map: map,
position: location,
title: m['title'],
icon: m['icon'],
});
zoneMarker.extend(marker.getPosition());
//Create event marker
google.maps.event.addListener(marker, 'click', function() {
var infowindow = new google.maps.InfoWindow({
content: m['text']
});
var pos = new google.maps.Marker({
position: location
});
infowindow.open(map, pos);
});
// --- Altitude
function getElevation(location){
// --- Create an ElevationService
var elevator = new google.maps.ElevationService();
// Create a LocationElevationRequest object using the array's one value
var positionalRequest = {
'locations': locations
}
// Initiate the location request
elevator.getElevationForLocations(positionalRequest, function(results, status) {
if (status == google.maps.ElevationStatus.OK) {
if (results[0]) {
return results[0].elevation;
}
} else {
return '';
}
});
}
// --- Set GeoCode
function setGeocode()
{
$.each(marks, function (i, item){
if (item.gps.lat && item.gps.lon) {
// --- Do nothing
} else {
var alt = getElevation(item.location);
//$.get();
}
});
}
// --- Wait the document is ready
$(document).ready(function(){
// --- Start timer
timer = setInterval(waitGeoCode, timerSecondInterval);
// --- Get location and geocode address if needed
$.each(marks, function (i, item){
if (item.gps.lat && item.gps.lon) {
marks[i].location = new google.maps.LatLng(item.gps.lat, item.gps.lon);
markDone++;
} else {
geocoder.geocode({ address:item.address, region:'FR' }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
// --- Set location to the marker
marks[i].location = results[0].geometry.location;
marks[i].locationType = results[0].geometry.location_type;
marks[i].locationAddress = results[0].formatted_address
}
}
markDone++;
//console.log("GeoCode: " + status + ' - index: ' + i);
});
}
});
});
</script>