【问题标题】:Trouble appending d3 vectors to leaflet map无法将 d3 向量附加到传单地图
【发布时间】:2019-03-13 13:48:59
【问题描述】:

我正在尝试将点叠加在传单地图上。

我在 html 中创建 Map 元素,构建传单地图,读取数据。

这就是我被绊倒的地方。我想在地图上显示点 - 我已经在 d3 地图上成功显示了这些点,但我想在上面的传单地图上重新显示它们。而不是提取纬度/经度,正如我在 d3 + 传单示例中看到的那样,我想我只是使用我之前成功使用过的路径生成器函数,以便将点附加到传单。

这里的代码序列:

    <div id="map" class="sf" style="width: 600px; height: 400px"></div>

     function ready(error) {

         //Build Leaflet map
         L.mapbox.accessToken = 

'pk.eyJ1Ijoic3RhcnJtb3NzMSIsImEiOiJjaXFheXZ6ejkwMzdyZmxtNmUzcWFlbnNjIn0.IoKwNIJXoLuMHPuUXsXeug';
                var mapboxUrl = 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}';
                //var accessToken = 'pk.eyJ1Ijoic3RhcnJtb3NzMSIsImEiOiJjam13ZHlxbXgwdncwM3FvMnJjeGVubjI5In0.-ridMV6bkkyNhbPfMJhVzw';
                var map = L.map('map').setView([37.7701177, -122.40], 13);
                    mapLink = 
                '<a href="http://openstreetmap.org">OpenStreetMap</a>';
                 L.tileLayer(
        'https://api.mapbox.com/styles/v1/mapbox/dark-v9/tiles/{z}/{x}/{y}?access_token=' + L.mapbox.accessToken, {
            tileSize: 512,
            zoomOffset: -1,
            attribution: '© <a href="https://www.mapbox.com/feedback/">Mapbox</a> © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
        }).addTo(map);

  // Read in the json data          
 d3.json("data/SanFrancisco_CoWorkingTenants.json", 
 function(SFData) {
     var SFData = SFCoworking.features
     console.log(SFData) // this prints successfully
    })


  var mapSVG = d3.select( "#map").select("svg")
  mapG = mapSVG.append("g");

   // Define d3 projection
   var albersProjectionBay = d3.geoAlbers()
        .scale( 282000)
        .rotate ( [122.4077441,] )
        .center( [0, 37.7701177] )

    // Define d3 path
    var geoPathBayArea = d3.geoPath()
        .projection( albersProjectionBay );


   var SFData = SFCoworking.features
       console.log(SFData)

    // draw points on map with d3 path generator 
    var feature = mapG.selectAll("path")
            .data(SFCoworking.features)
            .enter().append("path")
            .style("stroke", "black")  
            .style("opacity", .6) 
            .style("fill", "red")
            .attr("r", 20)
            .attr( "d", geoPathBayArea )
            console.log(feature) // nothing prints!

        }

当 SFdata 出现在控制台中时,这些特征在打印到控制台时只是显示为一个空数组。这让我相信我如何将 svg 元素附加到地图上可能存在问题?

【问题讨论】:

  • 你有你最后的代码 sn-p INSIDE json 加载回调,你在第三个 sn-p 中执行 console.log(SFData)
  • rio - 不确定我理解你的问题吗?
  • 您显示的是 sn-ps,但不显示它们如何相互连接,或者它们是否是 sub-sn-ps,如果它们只是顺序的,您将永远不会在地图上看到任何东西(异步加载)跨度>
  • 里约 - 谢谢。我已经在上面编辑了我的问题,所以希望代码序列更清晰。这是基本代码以及它如何协同工作。
  • 把所有的东西都放在 d3.json 回调中

标签: javascript jquery d3.js svg leaflet


【解决方案1】:

谢谢大家 - 这似乎工作。

        var svgLayer = L.svg();
            svgLayer.addTo(map);

            var svgMap = d3.select("#map").select("svg");
            var g = svgMap.select('g');

        d3.json("data/SanFrancisco_CoWorkingTenants.json", function(SFData) {
            var SFData = SFCoworking.features
            console.log(SFData)
            })
        SFData.forEach(function(d) {
            d.latLong = new L.LatLng(d.properties.Latitude,
                                    d.properties.Longitude);
            //console.log(d.LatLng)
        })
        var feature = g.selectAll("circle")
            .data(SFData)
            .enter().append("circle")
            .style("stroke", "black")
            .style("opacity", .4)
            .style("fill", "red")
            .attr("r", 20)
            .attr("class", 'features')

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 2016-07-22
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    相关资源
    最近更新 更多