This is a simple Leaflet map using a static GeoJSON file (BaseBallFinal.json) I use JQuery's getJSON function to read the file. I start with the basic map view and basemap, here I use osm (OpenStreetMap), define what the layer is going to look like, I chose circleMarker, and define the popup format and data. Last I read the GeoJSON file and add the data to the layer. This map is similar to my second map but now I have extra code to define colors for the selected record.
<!DOCTYPE html> <html> <head> <title>GeoJSON tutorial - Leaflet</title> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<style> #map { width: 800px; height: 600px; } </style>
</head> <body> <div id="map" > </div> <script> var url = 'BaseBallFinal.json'; // my GeoJSON data source, in the same folder as my HTML file.
var map = L.map('map').setView([37.52715,-96.877441], 4); var osm=new L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',{ attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'});
// Set function for color ramp function getColor(league){ return league == 'NL' ? 'blue' : league == 'AL' ? 'red' : 'white'; }
function forEachFeature(feature, layer) { layer.on('click', function (e) { // get coordinates from GeoJSON var coords = e.target.feature.geometry.coordinates //pass coords to function to create marker.(Yellow circle) onMapClick(coords); }); var popupContent = "<p>The <b>" + feature.properties.Team + "</b> play here,</br> They are in the " + feature.properties.League + "</br>" + '<a href="'+ feature.properties.Website +'" target="_blank">Website</a></p>' ; if (feature.properties && feature.properties.popupContent) { popupContent += feature.properties.popupContent; } layer.bindPopup(popupContent); };
var bbTeam = L.geoJSON(null, { onEachFeature: forEachFeature, pointToLayer: function (feature, latlng) { return L.circleMarker(latlng, { radius:6, opacity: .5, //color: "#000", color:getColor(feature.properties.League), fillColor: getColor(feature.properties.League), fillOpacity: 0.8 }).bindTooltip(feature.properties.Name); } });
// Get GeoJSON data and create features. $.getJSON(url, function(data) { bbTeam.addData(data); }); bbTeam.addTo(map);
// click marker var clickmark; // When you click on a circleMarker, it calls the onMapClick function and passes the layers coordinates. // I grab the coords which are X,Y, and I need to flip them to latLng for a marker, function onMapClick(coords) { console.log(coords); var thecoords = coords.toString().split(','); var lat = thecoords[1]; var lng = thecoords[0]; if (clickmark != undefined) { //if prior marker exists, remove it. map.removeLayer(clickmark); }; clickmark = L.circleMarker([lat,lng],{ radius: 8, color: "yellow", fillColor: "yellow", fillOpacity: 1} ).addTo(map); } // end of code for click marker.
</script> </body> </html>