Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

leaflet - importing iconurl from a csv in mapbox

I want to import a number of markers onto a mapbox map, where a csv file defines the name of the icon to use. The csv table looks like this:

pID,qID,longitude,latitude,bearing(degree),orientation(36),color,icon
PID1,QID35,90.39210677,23.7756582,80.1120824,8,3,3.png
PID1,QID40,90.39045721,23.77525565,216.2854365,22,6,6.png

I am using omnivore to convert this csv to geojson (at least I think that's what omnivore is doing)

The code I'm using is down below:


<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Custom marker styles for imported data</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v1.6.4/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v1.6.4/mapbox.css' rel='stylesheet' />
<style>
  body { margin:0; padding:0; }
  #map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-omnivore/v0.2.0/leaflet-omnivore.min.js'></script>

<div id='map'></div>

<script>
var southWest = L.latLng(23.7, 90.2),
    northEast = L.latLng(23.9, 90.7),
    bounds = L.latLngBounds(southWest, northEast);

var map = L.mapbox.map('map', 'bakr89.imia9old', {
    maxBounds: bounds,
    maxZoom: 17,
    minZoom: 14
}).setView([23.775507, 90.3909], 16);

// Omnivore will AJAX-request this file behind the scenes and parse it:
// note that there are considerations:
// - The CSV file must contain latitude and longitude values, in column
//   named roughly latitude and longitude
// - The file must either be on the same domain as the page that requests it,
//   or both the server it is requested from and the user's browser must
//   support CORS.

var csvdata = omnivore.csv('csvmarker.csv'),


var myLayer = L.mapbox.featureLayer(csvdata).addTo(map)
var myIcon = L.icon({
        iconUrl: 'csvdata.icon',
        iconSize: [40, 40],
        iconAnchor: [20, 20],
        popupAnchor: [0, -25],
    });
    // Set a custom icon on each marker based on feature properties.
    myLayer.on('layeradd', function(e) {
        var marker = e.layer,
            feature = marker.feature;

        marker.setIcon(L.icon(feature.properties.icon));
    }
    );

    // Add features to the map.
    myLayer.setGeoJSON(csvdata);
</script>
</body>
</html>

I'm not a coder, and I've been messing around with mapbox for about a week. Do you guys think you can help with seeing what's missing in this code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Proper use of omnivore in the case of csv is like that

var southWest = L.latLng(23.7, 90.2),
    northEast = L.latLng(23.9, 90.7),
    bounds = L.latLngBounds(southWest, northEast);

var map = L.mapbox.map('map', 'bakr89.imia9old', {
    maxBounds: bounds,
    maxZoom: 17,
    minZoom: 14
}).setView([23.775507, 90.3909], 16);

omnivore.csv('csvmarker.csv').addTo(map);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...