从mapbox中的csv导入iconurl [英] importing iconurl from a csv in mapbox

查看:331
本文介绍了从mapbox中的csv导入iconurl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些标记导入到mapbox地图上,其中csv文件定义要使用的图标的名称。 csv表如下所示:

  pID,qID,经度,纬度,方位,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



我使用omnivore将这个csv转换为geojson(至少我认为这是omnivore所做的)



我使用的代码如下:






 <!DOCTYPE html> 
< html>
< head>
< meta charset = utf-8 />
< title>导入数据的自定义标记样式< / 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会AJAX请求这个文件后台并解析它:
//注意有注意事项:
// - CSV文件必须包含纬度和经度值,在列
//命名大致纬度和经度
// - 该文件必须在与请求的页面相同的域,
//或两者的服务器并且用户的浏览器必须
//支持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],
});
//根据要素属性在每个标记上设置自定义图标。
myLayer.on('layeradd',function(e){
var marker = e.layer,
feature = marker.feature;

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

//向地图添加要素。
myLayer.setGeoJSON(csvdata);
< / script>
< / body>
< / html>






我不是一个编码器,一直在搞乱mapbox约一个星期。

解决方案

在csv的情况下正确使用omnivore是

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

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);


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.

解决方案

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);

这篇关于从mapbox中的csv导入iconurl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆