How do I add leaflet.markercluster plugin to my code? - leaflet

I’m wondering if anyone can help me.
I’m pretty new to coding and i’m trying to implement leaflets marker cluster into my site, however nothing is showing up when I add the usage from GitHub.
var BPalace = L.icon({
iconUrl: 'img/icons8-marker-50.png',
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [1, -30],
});
var marker1 = L.marker([51.5015385807725, -0.14176521957406812], {
icon: BPalace
}).bindPopup('Buckingham Palace');
var BatterseaPark = L.icon({
iconUrl: 'img/icons8-marker-50.png',
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [1, -30],
});
var marker2 = L.marker([51.478988287721876, -0.15648732182296068], {
icon: BatterseaPark
}).bindPopup('Battersea Park');
var wembley = L.icon({
iconUrl: 'img/icons8-marker-50.png',
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [1, -30],
});
var marker3 = L.marker([51.55619811605852, -0.2794567696109353], {
icon: wembley
}).bindPopup('Wembley Stadium');
var bAirport = L.icon({
iconUrl: 'img/icons8-marker-50.png',
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [1, -30],
});
var marker4 = L.marker([52.452333381857784, -1.7435316209846132], {
icon: bAirport
}).bindPopup('Birmingham Airport');
L.featureGroup([marker1, marker2, marker3, marker4])
.addTo(mymap);
var markers = L.markerClusterGroup();
markers.addLayer(L.marker(marker1(map)));
map.addLayer(markers).addTo(mymap);
Is there something I’m doing wrong? I have added the relevant files to my html page, but when i refresh nothing seems to happen.
Any help will be much appreciated.
Thank you

Your attempt was correct but the code not, change it to:
var markers = L.markerClusterGroup();
marker1.addTo(markers);
marker2.addTo(markers);
marker3.addTo(markers);
marker4.addTo(markers);
markers.addTo(mymap);
And remove:
L.featureGroup([marker1, marker2, marker3, marker4])
.addTo(mymap);

Related

Leaflet custom icon, react-leaflet, can't build Gatsby project

I've got custom icons working in my Leaflet map just fine when I run gatsby develop locally, but when I try to build I get an error related to my custom marker icon:
WebpackError: TypeError: leaflet_src_layer_marker__WEBPACK_IMPORTED_MODULE_4__.Icon is not a constructor
Related imports:
import { Map, Marker, Popup, TileLayer, Tooltip, ZoomControl } from 'react-leaflet'
import { Icon } from 'leaflet/src/layer/marker'
Create custom icon:
// Init custom map icon
const mapIcon = MapIcon();
const markerIcon = new Icon({
iconUrl: mapIcon,
iconSize: [36, 36],
iconAnchor: [18, 18],
shadowSize: [36, 36],
shadowUrl: 'https://unpkg.com/leaflet#1.5.1/dist/images/marker-shadow.png',
shadowAnchor: [10, 18],
popupAnchor: [0, -16],
tooltipAnchor: [13, -4]
});
JSX:
{ markerData.length > 0 ? markerData.map((node, index) => (
<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={markerIcon}>
<Popup className="leaflet-popup" onOpen={(el) => openPopup(el)}>
<h5 className="popup-location-title">{node.location_title}</h5>
<h6 className="popup-address">{node.address}</h6>
<div className="popup-description" dangerouslySetInnerHTML={{ __html: node.description }}></div>
{!!node.embed ?
<div className="popup-embed">
<Embed url={node.embed} className="popup-media-embed" />
</div>
: null}
</Popup>
<Tooltip className="leaflet-tooltip">
<span className="title">{node.location_title}</span>
<span className="address">{node.address}</span>
</Tooltip>
</Marker>
)) : null }
I've tried all the solutions listed here: https://github.com/Leaflet/Leaflet.markercluster/issues/874. I've also looked at several similar questions. Nothing seems to help. For instance, I also tried importing leaflet like so import * as L from "leaflet" and like so import L from "leaflet". And then creating the new icon like so const markerIcon = L.Icon({, like so const markerIcon = L.icon({, like so const markerIcon = new L.Icon({, and like so const markerIcon = new L.icon({. I can console.log(L), but none of these works.
I would love some assistance.
Got it to build. I had to wrap the icon construction in a check for the window obj:
// Init custom map icon
if (typeof window !== 'undefined') {
const mapIcon = MapIcon();
markerIcon = new Icon({
iconUrl: mapIcon,
iconSize: [36, 36],
iconAnchor: [18, 18],
shadowSize: [36, 36],
shadowUrl: 'https://unpkg.com/leaflet#1.5.1/dist/images/marker-shadow.png',
shadowAnchor: [10, 18],
popupAnchor: [0, -16],
tooltipAnchor: [13, -4]
});
}
And again when passing the icon to the marker:
<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={(!!markerIcon) ? markerIcon : null}>

Replace a showR2 with a custom text in a Google Chart?

I am playing around with Google Chart to look a certain way. In this situation I have a combo chart a line and column chart.
I have stumble upon a view "layout" problems
How do replace the show2r legend with just some custom text? At
the moment says: y = 2.032E-4 * x - 8.203 r^2 = 7.005E-3 and I want
to replace it with "Trendline (Lineair)
2/ Also the legend gets a
1/2 and Arrows left and right. I like the legend to always be
visible?
3/ The x axis doesn't display all dates, how can I set that
as a default?
4/ How do I add vertical line in say month June??
Regards
to change the trendline label in the legend, use option --> labelInLegend
there are no standard options to change the value in the tooltip,
but it can be changed manually using event --> onmouseover
when the legend's position is top,
you can use option --> legend.maxLines
to increase the number of lines available and prevent the arrows...
to ensure all dates are shown on the x-axis,
allow enough room by using option --> chartArea.bottom
see following working snippet for examples of each...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['x', 'y0', 'y1'],
[new Date(2017, 11, 28), 175, 10],
[new Date(2017, 11, 29), 159, 20],
[new Date(2017, 11, 30), 126, 35],
[new Date(2017, 11, 31), 129, 40],
[new Date(2018, 0, 1), 108, 60],
[new Date(2018, 0, 2), 92, 70]
]);
var options = {
chartArea: {
bottom: 72
},
hAxis: {
slantedText: true
},
height: 400,
legend: {
maxLines: 2,
position: 'top'
},
tooltip: {
isHtml: true
},
trendlines: {
0: {
labelInLegend: '0-Linear Trend',
showR2: true,
type: 'linear',
visibleInLegend: true
},
1: {
labelInLegend: '1-Linear Trend',
showR2: true,
type: 'linear',
visibleInLegend: true
}
},
width: 400
};
var container = document.getElementById('chart_div');
var chart = new google.visualization.ColumnChart(container);
google.visualization.events.addListener(chart, 'onmouseover', function (props) {
var tooltipLabels = container.getElementsByTagName('span');
for (var i = 0; i < tooltipLabels.length; i++) {
if (tooltipLabels[i].innerHTML.indexOf('y =') > -1) {
tooltipLabels[i].innerHTML = 'CUSTOM TEXT:';
}
}
});
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

fuzzy Popup in leaflet Maps

I am creating a map with leaflet, but the text of the popup appear fuzzied. ( Windows 7 Enterprise / Leaflet 0.7.7 / Google Chrome Version 51.0.2704.79 m)
<script>
var mymap = L.map('mapid', {
center: [42.9050205, 1.8760075],
zoom: 9
});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFsfdNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
maxZoom: 18,
attribution:
'museodelprado © www.museodelprado.es',
id: 'mapbox.streets'
}).addTo(mymap);
var MuseosIcon = L.Icon.extend({
options: {
shadowUrl: L.Icon.Default.imagePath + '/marker-shadow.png',
iconSize: [38, 95],
shadowSize: [50, 64],
iconAnchor: [22, 94],
shadowAnchor: [4, 62],
popupAnchor: [-3, -76]
}
});
var greenIcon = new MuseosIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-green.png'}),
redIcon = new MuseosIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-red.png'}),
blueIcon = new MuseosIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-blue.png'}),
yellowIcon = new MuseosIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-yellow.png'});
L.marker([42.13675,1.93143],{icon: blueIcon,title: 'MAQ. 1'}).addTo(mymap)
.bindPopup( "The Museo del Prado is the main Spanish national art museum, located in central Madrid. <br> It features one of the world's finest collections of European art, <br> dating from the 12th century to the early 19th century, <br> based on the former Spanish Royal Collection").openPopup();
var popup = L.popup();
</script>
my browser zoom was not at 100% (press Ctrl+zero)

custom markers not displaying on mapbox map

I am working with a mapbox map on a web page, and have everything working fine with the map and dropping pins, but cant get custom markers working.
My code that is working for dropping a pin is:
L.mapbox.featureLayer({
type: 'Feature',
geometry: { type:'Point', coordinates:[$longitude, $latitude] },
properties: { title:'My Location', description:'My Description', 'marker-size':'large', 'marker-color':'#cc0000', 'marker-symbol':'building' }
}).addTo(map);
This works and populates the map with my locations.
I have used this code to try and add a custom marker, but this doesnt work:
L.mapbox.featureLayer({
type: 'Feature',
geometry: { type:'Point', coordinates:[$longitude, $latitude] },
properties: { title:'My Location', description:'My Description', icon: { iconUrl: 'https://www.mapbox.com/mapbox.js/assets/images/astronaut1.png', iconSize: [50, 50], iconAnchor: [25, 25], popupAnchor: [0, -25], className: 'dot' } }
}).addTo(map);
I also have multiple pins and other layers on the map that can be turned on or off, and everything is working well, I just cant get this custom marker to work at all!
Does anyone have custom markers working with code like this on mapbox?
I don't think you can specify custom marker options like icon like that in the main GeoJSON properties hash.
I believe you would need to do something like the below instead.
var customIcon = new L.Icon({iconUrl: 'https://www.mapbox.com/mapbox.js/assets/images/astronaut1.png', iconSize: [50, 50], iconAnchor: [25, 25], popupAnchor: [0, -25], className: 'dot' })
L.mapbox.featureLayer(
{
type: 'Feature',
geometry: { type:'Point', coordinates:[$longitude, $latitude] },
properties: { title:'My Location', description:'My Description'}
},
{
pointToLayer: function(feature,latLng) { return L.marker(latLng, {icon: customIcon}}
}).addTo(map);
See https://www.mapbox.com/mapbox.js/api/v2.2.2/l-mapbox-featurelayer/

Draw Google Line Chart with multiple missing values

I copied this code from Google Line Chart reference and made some small changes:
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Dag');
data.addColumn('number', 'Målvikt');
data.addColumn('number', 'Uppmätt vikt');
data.addRows([
[1, 37.8, 55.0],
[2, null, 69.5],
[3, null, 57],
[4, null, 18.8],
[5, null, 17.6],
[6, null, 13.6],
[7, null, 12.3],
[8, null, 29.2],
[9, null, 42.9],
[10, null, 30.9],
[11, null, 7.9],
[12, null, 8.4],
[13, null, 6.3],
[14, 30.8, 6.2]
]);
var options = {
chart: {
title: 'Box Office Earnings in First Two Weeks of Opening',
subtitle: 'in millions of dollars (USD)',
interpolateNulls: true
},
width: 900,
height: 500
};
var chart = new google.charts.Line(document.getElementById('linechart_material'));
chart.draw(data, options);
}
My first line is not generated at all.
As you see, I want to just give the first and last value for the curve named "Målvikt" and draw a straight line between them. I found this related question and added interpolateNulls: true but actually it did not solve my problem.
I then changed all nulls except one to some value, but there still was no line between its neighbors. What am I doing wrong?
It seems that google.charts.Line component does not support interpolateNulls option.
Secondly, there is typo in specifying interpolateNulls option.
Since interpolateNulls property does not belong to chart property according to Configuration Options, the line:
var options = {
chart: {
interpolateNulls: true
}
};
should be replaced with:
var options = {
interpolateNulls: true
};
Having said that, i would recommend to utilize google.visualization.LineChart from corechart package instead of google.charts.Line component from line package. In that case interpolateNulls option could applied as demonstrated below:
Working example
google.load('visualization', '1.1', { packages: ['corechart'] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Dag');
data.addColumn('number', 'Målvikt');
data.addColumn('number', 'Uppmätt vikt');
data.addRows([
[1, 37.8, 55.0],
[2, null, 69.5],
[3, null, 57],
[4, null, 18.8],
[5, null, 17.6],
[6, null, 13.6],
[7, null, 12.3],
[8, null, 29.2],
[9, null, 42.9],
[10, null, 30.9],
[11, null, 7.9],
[12, null, 8.4],
[13, null, 6.3],
[14, 30.8, 6.2]
]);
var options = {
title: 'Box Office Earnings in First Two Weeks of Opening',
subtitle: 'in millions of dollars (USD)',
interpolateNulls: true,
width: 900,
height: 500
};
//var chart = new google.charts.Line(document.getElementById('linechart_material'));
var chart = new google.visualization.LineChart(document.getElementById('linechart_material'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="linechart_material" style="width: 640px; height: 480px"></div>