Last active
January 25, 2022 21:17
-
-
Save bertt/4956504 to your computer and use it in GitHub Desktop.
Using Leaflet in TypeScript. Original code see http://leafletjs.com/examples/quick-start-example.html
This file contains hidden or 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
// http://leafletjs.com/examples/quick-start-example.html in TypeScript | |
/// <reference path="leaflet.d.ts" /> | |
var map = new L.Map("map"); | |
map.setView(new L.LatLng(51.505, -0.09), 13); | |
var layer =new L.TileLayer("http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png", { maxZoom: 18, attribution: "attribution test" }) | |
layer.addTo(map); | |
// add marker | |
var marker = new L.Marker(new L.LatLng(51.5, -0.09)); | |
marker.addTo(map).bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup(); | |
// add circle | |
var circle = new L.Circle(new L.LatLng(51.508, -0.11), 500, { | |
color: 'red', | |
fillColor: '#f03', | |
fillOpacity: 0.5 | |
}).addTo(map).bindPopup("I am a circle."); | |
// add polygon | |
var latlongs : L.LatLng[]; | |
latlongs= [ | |
new L.LatLng(51.509, -0.08), | |
new L.LatLng(51.503, -0.06), | |
new L.LatLng(51.51, -0.047) | |
]; | |
var polygon = new L.Polygon(latlongs).addTo(map).bindPopup("I am a polygon."); | |
// popup on mapclick | |
var popup = new L.Popup(); | |
function onMapClick(e) { | |
popup | |
.setLatLng(e.latlng) | |
.setContent("You clicked the map at " + e.latlng.toString()) | |
.openOn(map); | |
} | |
map.on('click', onMapClick); |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>TypeScript HTML App</title> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.css" /> | |
<script src="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.js"></script> | |
</head> | |
<body> | |
<div id="map" style="width: 600px; height: 400px"></div> | |
<script src="map.js"></script> | |
</body> | |
</html> |
what is that goddamn L?my typescript does not understand it.
you can do:
$ npm install --save @types/leaflet
and change the first line
from:
/// <reference path="leaflet.d.ts" />
to:
/// <reference path="./node_modules/@types/leaflet/index.d.ts" />
Now compile and start a webserver:
$ tsc map.ts
$ python3 -m http.server
And browse to http://localhost:8000/
I do appreciate your reply. I will give it a try.
Thank you for the leaflet example, Bertt!
Thanks for an example.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where would I find the leaflet.d.ts to get this running. The one in Definitely Typed requires geojson .