I am following leaftlet js getting started tutorial. But I am unable to load the map. Here is the code I have
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet#1.0.2/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet#1.0.2/dist/leaflet.js"></script>
</head>
<style>
#mapid { height: 180px; }
</style>
<script>
var mymap = L.map('mapid').setView([51.505, -0.09], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA, Imagery © Mapbox',
maxZoom: 18,
id: 'your.mapbox.project.id',
accessToken: 'I have put public access token'
}).addTo(mymap);
</script>
<body>
<div style="margin-top:100px;margin-left:100px;height: 200px;width:200px" id="mapid"></div>
<div style="height:200px;width:200px;background: red">
</div>
</body>
</html>
In the above code, id: 'your.mapbox.project.id' I am unable to find this. I found only public access token, is this the reason map is not getting loaded .. any help is appreciated
To learn leaflet, you can just use the options of the example (check the source)
<div id="mapid" style="width: 600px; height: 400px;"></div>
<script>
var mymap = L.map('mapid').setView([51.505, -0.09], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
maxZoom: 18,
attribution: 'Map data © OpenStreetMap contributors, ' +
'CC-BY-SA, ' +
'Imagery © Mapbox',
id: 'mapbox.streets'
}).addTo(mymap);
</script>
Later on, when you are more familiar with leaflet, you can open a mapbox account and learn about what they are adding.
The reason why mapbox is mentioned in the home page of leaflet is because the creator of leaflet is now working for mapbox.
Related
I'm just trying to do a simple map using the Leaflet.js libraries directly instead of the python wrappers(ipyleaflet, folium) but I can't any map to display regardless of what I do. the map area is drawn the correct size with the zoom controls, as are the tile attributions, and a marker in the right location. I have tried multiple browsers and also in QWebEngineView as well as different tile sets. I've also tried both the hosted js and css scripts as well as local copies The results are the same. Everything with the same v1.7.1 release though.
Here is the html code:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="leaflet.css" />
<script src="leaflet.js"></script>
<!-- <link rel="stylesheet" href="https://unpkg.com/leaflet#1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="" />
<script src="https://unpkg.com/leaflet#1.7.1/dist/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin=""></script>
-->
<style>
#map {
width: 900px;
height: 400px;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var layer = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © OpenStreetMap contributors, Imagery © Mapbox',
maxZoom: 18,
id: 'mapbox/satellite-v9',
tileSize: 512,
zoomOffset: -1,
accessToken: 'your.mapbox.access.token'
});
var mymap = L.map('map', { preferCanvas: true, center: [43.08695, -73.75792], layers: [layer], zoom: 10 });
var mymarker = L.marker([43.08695, -73.75792], { draggable: true }).addTo(mymap)
</script>
</body>
</html
Here's a screenshot
Anybody got any ideas? This ought to be something really simple...
I am quiet new to Web Services and I am facing a problem.
I have a Map linked with a WMS.
I added a basemap and my WMS Date as a tileLayer and the show up correctly on my map.
Now I want the nearest Feature to PopUp when I click anywhere on the map.
I already searched the Web but could not find a solution I understood. (https://gist.github.com/rclark/6908938)
Can anyone help me with this?
This is my code so far:
<!DOCTYPE html>
<html>
<head>
<title>XXX</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet#1.6.0/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet#1.6.0/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<style type="text/css">
#nrwMap { height: 800px; width: 1300px;}
</style>
</head>
<body>
<div id="Map"></div>
<script type="text/javascript">
var mymap = L.map('Map').setView([51.28, 7.33], 8);
var url = 'http://www.xxx.xxx.com/wms/';
var basemap = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © OpenStreetMap contributors, ' +
'CC-BY-SA, ' +
'Imagery © Mapbox',
id: 'mapbox/streets-v11'
})
basemap.addTo(mymap);
var featureLayer = L.tileLayer.wms(url, {
layers: 'layerName',
format: 'image/png',
transparent: true
});
featureLayer.addTo(mymap);
</script>
</body>
</html>
Leaflet.GeometryUtil has a function/feature called closestLayer. It might help.
mymap.on('click', onMapClick);
function onMapClick(e) {
nearestLayer = L.GeometryUtil.closestLayer(mymap, layersToSearch, e.latlng)
// It will return the layer, latlng of point on layer nearest to the 'click' and distance from the 'click' }
I'm using SmartyBC on version 3.1.33. In the smarty template the code of leaflet and openstreetmap will not run. The map will not open.
I've all tested with or without the script between {literal} {/literal}. The map will not open. However, in a normally html site the code works perfectly. What's the mistake?
<html>
<head><link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet#1.5.1/dist/leaflet.css">
<script src='https://unpkg.com/leaflet#1.5.1/dist/leaflet.js'></script>
#map {
height: 500px;
}
</head>
<body>
<div id="map"></div>
<script>
{literal}
var map = L.map('map').setView([43.64701, -79.39425], 15);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
L.marker([51.5, -0.09]).addTo(map)
.bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
.openPopup();
{/literal}
</script>
</body>
</html>
No error messages and no map. Thanks for help.
You didn't put #map styles in HTML <style>. I did that and the same code without {literal} tags worked.
https://codepen.io/anon/pen/vqVLjL
The baselayers-control in leaflet is not activated, when a map is shown more than one time.
I wrote a test-program to show the problem:
<head>
<script>
'use strict';
var BING_KEY = "AplTrT4uzwlmfcERFFQu_NqDycERC_Er0qGYzZhIqrDfq-naYCsUr1kbbKRUqhq1";
var osmUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
function show_karte(div_id, mittelpunkt, zoom){
var bingLayer = L.tileLayer.bing({bingMapsKey: BING_KEY, maxZoom: 20, imagerySet: 'Aerial', attribution: '© 2019 Microsoft Corporation, Earthstar Geographics SIO'});
var bingLabelLayer = L.tileLayer.bing({bingMapsKey: BING_KEY, maxZoom: 20, imagerySet: 'AerialWithLabels', attribution: '© 2019 Microsoft Corporation, Earthstar Geographics SIO'});
var osmLayer = L.tileLayer(osmUrl, {maxZoom: 20, attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA'});
var mittelpunkt = [mittelpunkt[1], mittelpunkt[0]]
var map = L.map(div_id,{layers: [bingLabelLayer]}).setView(mittelpunkt, zoom);
var baseLayers = {"Straßenkarte": osmLayer, "Luftbild mit Straßennamen": bingLabelLayer,"Luftbild": bingLayer};
var control = L.control.layers(baseLayers).addTo(map);
};
</script>
</head>
<body>
<div id="map" style="width:450px; height: 450px;">
<script type="text/javascript"> show_karte("map",[6.026173,50.816022],13); </script>
</div>
<div id="map1" style="width:550px; height: 450px;">
<script type="text/javascript"> show_karte("map1",[6.10428,50.76079],11); </script>
</div>
<div id="map2" style="width:650px; height:450px">
<script type="text/javascript">show_karte("map2",[6.10428,50.76079],12); </script>
</div>
</body>
You can see the program here:
https://aachen-hat-energie.de/test_ww/test_zukunft.htm
The control is only activated on the third map. Where is the error?
I am trying to display my dronedeploy map with leaflet.js, but the link which is generated by the Leaflet Tile Layer market app gives me an access denied error.
I am on a 30 day trial account but I have a few weeks remaining.
Also, is this the best way to display the data in leaflet? I noticed you can export the map as GeoTiff tiles, but I cannot find any examples on how to import/display these in leaflet
Okay so after replacing the {z}/{x}/{y}.png in the url with valid tile numbers, I got back some results.
I used this page to calculate a valid tile number:
http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
However this was unnecessary to get my map displayed in leaflet.
All I had to do was use the url when defining a tile layer, here's a complete example:
<!DOCTYPE html>
<html>
<head>
<title>Quick Start - Leaflet</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
<link rel="stylesheet" href="https://unpkg.com/leaflet#1.0.3/dist/leaflet.css" integrity="sha512-07I2e+7D8p6he1SIM+1twR5TIrhUQn9+I6yjqD53JQjFiMf8EtC93ty0/5vJTZGF8aAocvHYNEDJajGdNx1IsQ==" crossorigin=""/>
<script src="https://unpkg.com/leaflet#1.0.3/dist/leaflet.js" integrity="sha512-A7vV8IFfih/D732iSSKi20u/ooOfj/AGehOKq0f4vLT1Zr2Y+RX7C+w8A1gaSasGtRUZpF/NZgzSAu4/Gc41Lg==" crossorigin=""></script>
</head>
<body>
<div id="mapid" style="width: 600px; height: 400px;"></div>
<script>
var mymap = L.map('mapid').setView([14.0, -8.2474], 13);
// add base map layer
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 22,
attribution: 'Map data © OpenStreetMap contributors, ' +
'CC-BY-SA, ' +
'Imagery © Mapbox',
id: 'mapbox.streets'
}).addTo(mymap);
// url generated from "Leaflet / Mapbox / Fulcrum Tile Layer" market place app on dronedeploy
// (url truncated)
var leafletTileLayerUrl = 'https://public-tiles.dronedeploy.com/1494366194_KUJOSCARTOPENPIPELINE_ortho_vdt/{z}/{x}/{y}.png?Policy=.....'
// add custom map layer
L.tileLayer(leafletTileLayerUrl, {
maxZoom: 22,
}).addTo(mymap);
</script>
</body>
</html>