Last active
January 12, 2023 07:32
-
-
Save stavros-melidoniotis/d8e8347b7b7a873f0d5d9b7ba8d1a1f2 to your computer and use it in GitHub Desktop.
Leaflet basic config for Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function enqueue_styles_and_scripts() | |
{ | |
wp_enqueue_script('leafletjs', get_template_directory_uri() . '/path/to/leaflet-example.js', array(), '1.0.0', true); | |
} | |
add_action('wp_enqueue_scripts', 'enqueue_styles_and_scripts'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
// content before | |
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" /> | |
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script> | |
// or if you've downloaded the leafletjs files | |
<link rel="stylesheet" href="<?= get_template_directory_uri() . '/path/to/leaflet.css' ?>" /> | |
<script src="<?= get_template_directory_uri() . '/path/to/leaflet.js' ?>"></script> | |
// content after | |
</head> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create the map element inside the div with id=map | |
const map = L.map('map', { | |
center: [<map_lat>, <map_lng>], | |
zoom: <map_zoom>, | |
}) | |
// add the openstreetmap tileLayer | |
L.tileLayer(`https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png`, { | |
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', | |
}).addTo(map) | |
// OPTIONAL: create a custom marker icon | |
const markerIcon = L.icon({ | |
iconUrl: 'path/to/custom_icon', | |
iconSize: [40, 40], | |
}) | |
// OPTIONAL: add the marker with custom icon to the map | |
L.marker([<marker_lat>, <marker_lng>], { icon: markerIcon }).addTo(map); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment