Skip to content
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>
<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="" type="text/css" />
<script src=""></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
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: '<Your Azure Maps Key>'
//Wait until the map resources are ready.'ready', function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
//Add a layer for rendering the data as symbols.
var layer = new atlas.layer.SymbolLayer(datasource, null, {
iconOptions: {
allowOverlap: true
//Add click events to the polygon and line layers.'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.
.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.
} else {
//Add the data to the data source.
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.
//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.;
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.
} else {
return html.join('');
<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="">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="" target="_blank">USGS Earthquake Hazards Program</a> and consists of all earthquakes with a magnitude of 7 or more between 1900 and 2018.
You can’t perform that action at this time.