Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

150 lines (123 sloc) 6.4 KB
<!DOCTYPE html>
<html>
<head>
<title>Add a delimited file (CSV) to the map - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=Edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to add a delimited file (CSV, TSV) to the map by converting it into GeoJSON." />
<meta name="keywords" content="map, gis, API, SDK, CSV, TSV, delimited file" />
<meta name="author" content="Microsoft Azure Maps" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.css" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.js"></script>
<!-- Add a reference to the csv2geojson library -->
<script src="/Common/scripts/csv2geojson.js"></script>
<script type='text/javascript'>
var map, popup, datasource;
var delimitedFileUrl = '/Common/data/earthquakes_gt7_alltime.csv';
function GetMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
view: 'Auto',
//Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at https://azure.com/maps
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: '<Your Azure Maps Key>'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Add a layer for rendering the data as symbols.
var layer = new atlas.layer.SymbolLayer(datasource, null, {
iconOptions: {
allowOverlap: true
}
});
map.layers.add(layer);
//Add click events to the polygon and line layers.
map.events.add('click', layer, featureClicked);
//Create a popup but leave it closed so we can update it and display it later.
popup = new atlas.Popup();
//Download the delimited file.
fetch(delimitedFileUrl)
.then(response => response.text())
.then(function (delimitedFile) {
//Parse the delimited file using the csv2geojson library.
csv2geojson.csv2geojson(delimitedFile, {
//Specify the fields names that contain the latitude and longitude values.
latfield: 'latitude',
lonfield: 'longitude',
//Specify the delimiter to parse the file with.
delimiter: ','
}, function (err, data) {
if (err) {
//Alert the user if there is any error in reading the delimited file.
alert(err);
} else {
//Add the data to the data source.
datasource.add(data);
}
});
});
});
}
function featureClicked(e) {
//Make sure the event occurred on a shape feature.
if (e.shapes && e.shapes.length > 0) {
var properties = e.shapes[0].getProperties();
//By default, show the popup where the mouse event occurred.
var pos = e.position;
var offset = [0, 0];
//If the shape is a point feature, show the popup at the points coordinate.
if (e.shapes[0].getType() === 'Point') {
pos = e.shapes[0].getCoordinates();
offset = [0, -18];
}
//Update the content and position of the popup.
popup.setOptions({
//Create a table from the properties in the feature.
content: '<div style="padding:10px;max-height:200px;overflow-y:auto;">' + object2Table(properties) + '</div>',
position: pos,
pixelOffset: offset
});
//Open the popup.
popup.open(map);
}
}
function object2Table(obj) {
//Create a HTML table from an objects property names and values.
var html = ['<table><tr><td><b>Property</b></td><td><b>Value</b></td><tr>'];
Object.keys(obj).forEach(function (key, index) {
//Ignore private properties which are commonly denoted using an underscore.
if (key.indexOf('_') !== 0) {
html.push('<tr><td>', key, '</td><td>');
if (typeof obj[key] === 'object') {
//If the value of the property is an object, create a sub-table recursively.
html.push(object2Table(obj[key]));
} else {
html.push(obj[key]);
}
html.push('</td><tr>');
}
});
html.push('</table>');
return html.join('');
}
</script>
</head>
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Add a delimited file (CSV) to the map</legend>
This sample shows how to add a delimited file (CSV, TSV) to the map by converting it into GeoJSON.
This sample uses the open source <a href="https://github.com/mapbox/csv2geojson">csv2geojson</a> library.
It is also possible to manually parse delimited files, but using a library provides more reusable functionality.
The data in this sample is sourced from the
<a href="https://earthquake.usgs.gov/" target="_blank">USGS Earthquake Hazards Program</a> and consists of all earthquakes with a magnitude of 7 or more between 1900 and 2018.
</fieldset>
</body>
</html>
You can’t perform that action at this time.