標題: 發布地圖leaflet開源 [打印本頁] 作者: Tiansijing 時間: 2018-5-29 13:21 標題: 發布地圖leaflet開源 實例代碼 .js
// Using Leaflet for creating the map and adding controls for interacting with the map
//
//--- Part 1: adding base maps ---
//
//creating the map; defining the location in the center of the map (geographic coords) and the zoom level. These are properties of the leaflet map object
//the map window has been given the id 'map' in the .html file
var map = L.map('map', {
center: [22, 100.5],
zoom: 9
});
//adding two base maps
var landscape = L.tileLayer('http://{s}.tile.thunderforest.com/landscape/{z}/{x}/{y}.png', {
attribution: 'Tiles from Thunderforest'});
var toner = L.tileLayer('http://tile.stamen.com/toner/{z}/{x}/{y}.png', {attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://www.openstreetmap.org/copyright">ODbL</a>' });
//toner.addTo(map);
// for using the two base maps in the layer control, I defined a baseMaps variable
var baseMaps = {
"Thunderforest landscape": landscape,
"Toner": toner,
"ESRI":esri,
}
//
//---- Part 4: adding line features from a geojson file
//
//adding a GeoJSON line feature set
/*var riverStyle = {
"color": "#0000ff",
"weight": 4
}
var river= L.geoJson(Salzach, {
style: riverStyle,
onEachFeature: function (feature, layer) {
layer.bindPopup('Salzach river' );
}
});
*/
//
//---- Part 5: Adding GeoJSON Point Features
//
//
//---- Adding GeoJSON point features - to marker object
//
/*
var summitsJson = {
"type":"FeatureCollection",
"features":[{"type":"Feature","properties":{"NAME":"Kreuzkogel","HEIGHT":2027},"geometry":{"type":"Point","coordinates":[13.153268433907614,47.22421002245328]}},
{"type":"Feature","properties":{"NAME":"Fulseck","HEIGHT":2034},"geometry":{"type":"Point","coordinates":[13.147417093794559,47.23423788545316]}},
{"type":"Feature","properties":{"NAME":"Kieserl","HEIGHT":1953},"geometry":{"type":"Point","coordinates":[13.152967420479607,47.24300413792524]}}]};
//
//---- Part 6: Adding a click event to our map
//
//when you click in the map, an alert with the latitude and longitude coordinates of the click location is shown
// e is the event object that is created on mouse click
/*
//the variable federalstateSBG is created in the Federalstates.js file
var river = L.geoJson(Salzach, {style: riverStyle});
*/
/*
var river= L.geoJson(Salzach, {
style: riverStyle,
onEachFeature: function (feature, layer) {
layer.bindPopup( 'ffffff' + feature.properties.name );
}
});
river.addTo(map);
*/
//
//---- Part 9: Adding a layer control for base maps and feature layers
//
//the variable features lists layers that I want to control with the layer control
var features = {
"Marker 2": mark,
//"river": river,
//"Salzburg": fedstate,
//"Summits": summits,
"Xishuang":xishuang,
"Forest": forest,
//"yunnan": yun
//"boundary": boundary
//"point2": point1
}
//the legend uses the layer control with entries for the base maps and two of the layers we added
//in case either base maps or features are not used in the layer control, the respective element in the properties is null
var legend = L.control.layers(baseMaps,features, {position:'topleft', collapsed:true}).addTo(map);