convert to leaflet
This commit is contained in:
@@ -55,6 +55,7 @@ oSpot.pageInit = function(asHash)
|
||||
else
|
||||
{
|
||||
var iMapRatio = 0.7;
|
||||
var iConfidenceInterval = 0.05;
|
||||
var iMinLat, iMaxLat, iMinLng, iMaxLng;
|
||||
iMinLat = iMinLng = 180;
|
||||
iMaxLat = iMaxLng = -180;
|
||||
@@ -66,20 +67,34 @@ oSpot.pageInit = function(asHash)
|
||||
});
|
||||
|
||||
//Get Marker bounds
|
||||
var oSouthWest = {lat:iMinLat, lng:iMinLng};
|
||||
var oNorthEast = {lat:iMaxLat, lng:iMaxLng};
|
||||
oMarkerBounds = new google.maps.LatLngBounds(oSouthWest, oNorthEast);
|
||||
var oSouthWest = L.latLng(iMinLat, iMinLng);
|
||||
var oNorthEast = L.latLng(iMaxLat, iMaxLng);
|
||||
oMarkerBounds = new L.LatLngBounds(oSouthWest, oNorthEast);
|
||||
agCenter = oMarkerBounds.getCenter();
|
||||
|
||||
//Calculate adequate zoom (map.fitBounds is dezooming too much)
|
||||
var oMapDim = {height: self.tmp('$Map').height()*0.95, width: self.tmp('$Map').width()*(iMapRatio - 0.05)};
|
||||
var oMapDim = {
|
||||
height: self.tmp('$Map').height()*(1 - iConfidenceInterval),
|
||||
width: self.tmp('$Map').width()*(iMapRatio - iConfidenceInterval)
|
||||
};
|
||||
iZoom = getBoundsZoomLevel(oMarkerBounds, oMapDim);
|
||||
}
|
||||
|
||||
var oMap = new google.maps.Map(self.tmp('$Map')[0], {
|
||||
//Tile layers
|
||||
var oMapBoxSat = L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}@2x.png?access_token='+self.vars('mapbox_key'), {id: 'mapbox.satellite', minZoom: 0, maxZoom: 19}),
|
||||
oMapBoxStreet = L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token='+self.vars('mapbox_key'), {id: 'mapbox.streets'}),
|
||||
oOpenTopoMap = L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {id: 'OpenTopoMap', minZoom: 2, maxZoom: 19});
|
||||
//oLinz = L.tileLayer('http://tiles-{s}.data-cdn.linz.govt.nz/services;key=92193c0af382409a98694d3e4aa8bd46/tiles/v4/layer=50767/EPSG:3857/{z}/{x}/{y}.png', {id: 'Linz', maxZoom: 17, continuousWorld: true, attribution: 'Sourced from LINZ. CC BY 4.0', subdomains:'abcd'});
|
||||
//oGoogleSatellite = L.tileLayer('https://mt.google.com/vt/lyrs=y&x={x}&y={y}&z={z}', {id: 'GoogleSatellite', minZoom: 1, maxZoom: 22});
|
||||
|
||||
//Map
|
||||
var oMap = L.map(self.tmp('$Map')[0], {
|
||||
center: agCenter,
|
||||
zoom: iZoom,
|
||||
mapTypeId: google.maps.MapTypeId.SATELLITE,
|
||||
layers: [oMapBoxSat/*, oMapBoxSreet, oOpenTopoMap, oLinz, oGoogleSatellite*/],
|
||||
attributionControl: false
|
||||
/*mapTypeId: google.maps.MapTypeId.SATELLITE,
|
||||
|
||||
scaleControl: true,
|
||||
scaleControlOptions: {
|
||||
position: google.maps.ControlPosition.TOP_LEFT
|
||||
@@ -97,92 +112,89 @@ oSpot.pageInit = function(asHash)
|
||||
fullscreenControlOptions: {
|
||||
position: google.maps.ControlPosition.LEFT_BOTTOM
|
||||
},
|
||||
streetViewControl: false
|
||||
streetViewControl: false*/
|
||||
});
|
||||
|
||||
var oKmlLayer = new google.maps.KmlLayer({
|
||||
url: 'http://spot.lutran.fr/kml/TeAraroaTrail_simplified.kmz?v3',
|
||||
preserveViewport: true,
|
||||
map: oMap
|
||||
|
||||
//Controls
|
||||
//L.control.layers({'Map Box': oMapBox, 'Open Topo Map': oOpenTopoMap/*, 'LINZ': oLinz, 'Google Satellite': oGoogleSatellite*/}).addTo(oMap);
|
||||
|
||||
//Te Araroa track
|
||||
$.ajax({
|
||||
dataType: 'json',
|
||||
url: 'kml/TeAraroaTrail_simplified.geojson',
|
||||
mimeType: 'application/json',
|
||||
success: function(data) {
|
||||
L.geoJson(data, {
|
||||
style: function(oTrack) {
|
||||
var sColor = '';
|
||||
switch (oTrack.properties.type) {
|
||||
case 'te_araroa': sColor = '#00ff78'; break;
|
||||
case 'routeburn': sColor = '#0000ff'; break;
|
||||
case 'hitchhiking': sColor = '#FF7814'; break;
|
||||
}
|
||||
return {color: sColor};
|
||||
}}).addTo(oMap);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//Building messages
|
||||
$.each(oMessages, function(iKey, oMsg){
|
||||
var oMarker = new google.maps.Marker({
|
||||
|
||||
//Marker
|
||||
var oMarker = L.marker(L.latLng(oMsg.latitude, oMsg.longitude), {
|
||||
id: oMsg.id_message,
|
||||
position: {lat:oMsg.latitude, lng:oMsg.longitude},
|
||||
//animation: google.maps.Animation.DROP,
|
||||
icon: (iKey%2==0)?'images/footprint_alt.png':'images/footprint.png',
|
||||
map: oMap,
|
||||
draggable: false
|
||||
riseOnHover: true,
|
||||
icon: L.icon({
|
||||
iconUrl: (iKey%2==0)?'images/footprint_alt.png':'images/footprint.png',
|
||||
iconSize: [32, 37],
|
||||
iconAnchor: [16, 37]
|
||||
})
|
||||
}).addTo(oMap);
|
||||
|
||||
//Marker events
|
||||
oMarker.on({
|
||||
mouseover: function(){
|
||||
|
||||
},
|
||||
mouseout: function(){
|
||||
|
||||
},
|
||||
click: function(oPoint){
|
||||
self.tmp('map').setView(oPoint.latlng, 15);
|
||||
}
|
||||
});
|
||||
|
||||
var $InfoWindow = $('<div>', {'class':'info-window'})
|
||||
//Tooltip
|
||||
$Tooltip = $('<div>', {'class':'info-window'})
|
||||
.append($('<h1>').append('Message '+oMsg.type+' #'+oMsg.id_message))
|
||||
.append($('<p>', {'class':'time'}).addIcon('fa-clock-o').append(oMsg.formatted_time+' ('+oMsg.relative_time+')'))
|
||||
.append($('<p>', {'class':'coordinates'}).addIcon('fa-compass').append('Lat : '+oMsg.latitude+', Lng : '+oMsg.longitude))
|
||||
.hover(
|
||||
function(){
|
||||
self.tmp('infowindow', true);
|
||||
},
|
||||
function(){
|
||||
self.tmp('infowindow', false);
|
||||
oInfoWindow.close(oMap, oMarker);
|
||||
}
|
||||
);
|
||||
//.append($('<p>', {'class':'battery'}).addIcon('fa-battery-three-quarters').append(oMsg.battery_state))
|
||||
.append($('<p>', {'class':'coordinates'}).addIcon('fa-compass').append('Lat : '+oMsg.latitude+', Lng : '+oMsg.longitude));
|
||||
|
||||
//Tooltip pictures
|
||||
if(oMsg.pics) {
|
||||
var $Pics = $('<div>', {'class':'pics'});
|
||||
$.each(oMsg.pics, function(iKey, asPic){
|
||||
$Pics.append($('<a>', {href: asPic.path, 'data-lightbox': self.consts.title, 'data-title': asPic.formatted_time})
|
||||
.append($('<img>', {'src': asPic.thumb_path})));
|
||||
});
|
||||
$InfoWindow
|
||||
$Tooltip
|
||||
.append($('<p>').addIcon('fa-image').append('Photos'))
|
||||
.append($Pics);
|
||||
}
|
||||
|
||||
var oInfoWindow = new google.maps.InfoWindow({
|
||||
content: $InfoWindow[0]
|
||||
});
|
||||
|
||||
oMarker.addListener('mouseover', function(){
|
||||
oInfoWindow.open(oMap, oMarker);
|
||||
});
|
||||
oMarker.addListener('mouseout', function(){
|
||||
setTimeout(function(){
|
||||
if(!self.tmp('infowindow')) oInfoWindow.close(oMap, oMarker);
|
||||
}, 400);
|
||||
});
|
||||
oMarker.addListener('click', function(){
|
||||
self.tmp('map').panTo(oMarker.getPosition());
|
||||
self.tmp('map').setZoom(15);
|
||||
oMarker.bindTooltip($Tooltip[0], {
|
||||
offset: [-16, -18],
|
||||
direction: 'left'
|
||||
});
|
||||
});
|
||||
|
||||
//Recenter map once loaded to be at the center of 70% (iMapRatio) of the page
|
||||
if(self.vars('mode')!='blog')
|
||||
{
|
||||
oProjListener = oMap.addListener('projection_changed', function() {
|
||||
|
||||
var iZoom = self.tmp('map').getZoom();
|
||||
|
||||
var oCenterLatLng = self.tmp('map').getCenter();
|
||||
var oCenterPoint = self.tmp('map').getProjection().fromLatLngToPoint(oCenterLatLng);
|
||||
|
||||
var iOffsetX = self.tmp('$Map').width()*(iMapRatio - 1)/2;
|
||||
var iOffsetPointX = new google.maps.Point(iOffsetX / Math.pow(2, iZoom), 0).x;
|
||||
|
||||
var oNewCenterPoint = new google.maps.Point(oCenterPoint.x - iOffsetPointX, oCenterPoint.y);
|
||||
var oNewCenterLatLng = self.tmp('map').getProjection().fromPointToLatLng(oNewCenterPoint);
|
||||
self.tmp('map').setCenter(oNewCenterLatLng);
|
||||
|
||||
oProjListener.remove();
|
||||
});
|
||||
}
|
||||
if(self.vars('mode')!='blog') offsetMap(oMap, iMapRatio);
|
||||
|
||||
//Legend
|
||||
oMap.controls[google.maps.ControlPosition.BOTTOM_RIGHT].push($('#legend')[0]);
|
||||
var oLegend = L.control({position: 'bottomleft'});
|
||||
oLegend.onAdd = function() {return L.DomUtil.get('legend');};
|
||||
oLegend.addTo(oMap);
|
||||
|
||||
self.tmp('map', oMap);
|
||||
});
|
||||
@@ -234,9 +246,9 @@ function getBoundsZoomLevel(bounds, mapDim) {
|
||||
var ne = bounds.getNorthEast();
|
||||
var sw = bounds.getSouthWest();
|
||||
|
||||
var latFraction = (latRad(ne.lat()) - latRad(sw.lat())) / Math.PI;
|
||||
var latFraction = (latRad(ne.lat) - latRad(sw.lat)) / Math.PI;
|
||||
|
||||
var lngDiff = ne.lng() - sw.lng();
|
||||
var lngDiff = ne.lng - sw.lng;
|
||||
var lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;
|
||||
|
||||
var latZoom = zoom(mapDim.height, WORLD_DIM.height, latFraction);
|
||||
@@ -245,6 +257,15 @@ function getBoundsZoomLevel(bounds, mapDim) {
|
||||
return Math.min(latZoom, lngZoom, ZOOM_MAX);
|
||||
}
|
||||
|
||||
//Offset map longitude by iMapRatio (%)
|
||||
function offsetMap(oMap, iMapRatio) {
|
||||
var oBounds = oMap.getBounds();
|
||||
var oCenter = oMap.getCenter();
|
||||
var iOffsetX = (oBounds.getEast() - oBounds.getWest())*(iMapRatio - 1)/2;
|
||||
oCenter.lng = oCenter.lng - iOffsetX;
|
||||
oMap.panTo(oCenter);
|
||||
}
|
||||
|
||||
function onFeedScroll(){
|
||||
var $Box = $(this);
|
||||
var $BoxContent = $Box.find('.simplebar-content');
|
||||
@@ -306,8 +327,7 @@ function getPost(asPost) {
|
||||
.data('lng', asPost.longitude)
|
||||
.click(function(){
|
||||
var $This = $(this);
|
||||
self.tmp('map').panTo({lat: parseFloat($This.data('lat')), lng: parseFloat($This.data('lng'))});
|
||||
self.tmp('map').setZoom(13);
|
||||
self.tmp('map').setView(L.latLng(parseFloat($This.data('lat')), parseFloat($This.data('lng'))), 13);
|
||||
})
|
||||
);
|
||||
sClass = 'compass';
|
||||
@@ -349,16 +369,13 @@ function getPost(asPost) {
|
||||
}
|
||||
|
||||
function getStaticMapUrl(oCenterLat, oCenterLng){
|
||||
var sDomain = 'https://maps.googleapis.com/maps/api/staticmap';
|
||||
var asParams = {
|
||||
center: oCenterLat+','+oCenterLng,
|
||||
zoom: '13',
|
||||
size: '400x300',
|
||||
maptype: 'satellite',
|
||||
markers: 'color:green|label:|'+oCenterLat+','+oCenterLng,
|
||||
key: self.vars('google_api')
|
||||
};
|
||||
|
||||
return sDomain+'?'+Object.keys(asParams).map(k => k+'='+asParams[k]).join('&');
|
||||
var asParams = [
|
||||
'https://api.mapbox.com/v4/mapbox.satellite', //Domain
|
||||
'url-http%3A%2F%2Fspot.lutran.fr%2Fimages%2Ffootprint.png('+oCenterLng+','+oCenterLat+')', //Marker
|
||||
oCenterLng+','+oCenterLat+',13', //Center + zoom
|
||||
'400x300.png?access_token='+self.vars('mapbox_key') //Image size + access token
|
||||
];
|
||||
|
||||
return asParams.join('/');
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user