adding leaflet marker to d3.js map

2019-06-25 13:13发布

问题:

I tried adding leaflet marker to d3'js map.Unfortunately the marker doesn't show. Is it possible to mix these two scripts? Here's my code:

<!DOCTYPE html>
<meta charset="utf-8">
 <link type="text/css" rel="stylesheet" href="d3map.css" />
<link rel="stylesheet" href="leaflet.css"/>
    <script src="leaflet.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/colorbrewer.v1.min.js"></script>

<script src="http://bl.ocks.org/emeeks/raw/f3105fda25ff785dc5ed/tile.js" type="text/javascript">
</script>
<script src="http://bl.ocks.org/emeeks/raw/f3105fda25ff785dc5ed/d3.quadtiles.js" type="text/javascript">
</script>
<script src="http://bl.ocks.org/emeeks/raw/f3105fda25ff785dc5ed/d3.geo.raster.js" type="text/javascript">
</script>
<script src="https://rawgit.com/emeeks/d3-carto-map/master/d3.carto.map.js" type="text/javascript">
</script>
<style>
svg{
    position: absolute;
}

 html,body {
    height: 100%;
    width: 100%;
    margin: 0;
  }

  #map {
    height: 500px;
    width: 960px;
    position: absolute;
  }

  .reproject {
    position: absolute;
    z-index: 99;
    left: 50px;
    top: 250px;
  }


  .metro {
    fill: rgba(255,0,255,.5);
    stroke: darkred;
    stroke-width: 1
  }
.markerButton {
    position: fixed;
    top: 20px;
    cursor: pointer;
    z-index: 99;
  }

body {
  background: #fcfcfa;
}

.stroke {
  fill: none;
  stroke: #000;
  stroke-width: 3px;
}

.fill {
  fill: #fff;
}

.graticule {
  fill: none;
  stroke: #777;
  stroke-width: .5px;
  stroke-opacity: .5;
}

.land {
  fill: #222;
}

.boundary {
  fill: none;
  stroke: #fff;
  stroke-width: .5px;
}

</style>
<body onload="makeSomeMaps()">




<script>

var width = 960,
    height = 500;

var projection = d3.geo.mollweide()
    .scale(165)
    .translate([width / 2, height / 2])
    .precision(.1);

var path = d3.geo.path()
    .projection(projection);

var graticule = d3.geo.graticule();

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);


svg.append("defs").append("path")
    .datum({type: "Sphere"})
    .attr("id", "sphere")
    .attr("d", path);

svg.append("use")
    .attr("class", "stroke")
    .attr("xlink:href", "#sphere");

svg.append("use")
    .attr("class", "fill")
    .attr("xlink:href", "#sphere");

svg.append("path")
    .datum(graticule)
    .attr("class", "graticule")
    .attr("d", path);

d3.json("world-50m.json", function(error, world) {
  if (error) throw error;

  svg.insert("path", ".graticule")
      .datum(topojson.feature(world, world.objects.land))
      .attr("class", "land")
      .attr("d", path);

  svg.insert("path", ".graticule")
      .datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
      .attr("class", "boundary")
      .attr("d", path);
});

d3.select(self.frameElement).style("height", height + "px");
var marker=L.marker([0,0],
             {draggable:true,

             });


marker.on('dragend',function(e){
             //alert(marker.getLatLng().toString());
             marker.getPopup().setContent('Clicked'+marker.getLatLng().toString()).openOn(map);
             });


function circleMarker() {
    var sizeScale = d3.scale.linear().domain([0,100,2000]).range([2,10,20]).clamp(true);
    var randomDatapoint = "r" + Math.ceil(Math.random() * 7);
    markers.append("circle").selectAll("*").remove();
    markers.append("circle")
    .attr("class", "metro")
    .attr("r", function(d) {return sizeScale(d[randomDatapoint])})
  }

function makeSomeMaps() {
    map = d3.carto.map();



d3.select("#map").call(map);
    map.centerOn([-99,39],"latlong");
    map.setScale(4);

    map.refresh();

cityLayer = d3.carto.layer.csv();
    cityLayer
    .path("cities.csv")
    .label("Metro Areas")
    .cssClass("metro")
    .renderMode("svg")
    .x("x")
    .y("y")
    .clickableFeatures(true)
    .on("load", function(){console.log(cityLayer.dataset())});

    map.addCartoLayer(cityLayer);

}



</script>
<div id="map">
<button style="left: 340px;" class="markerButton" onclick="circleMarker();">Circle Marker</button>
</div>

</body>
</html>

Here's live website:http://www.ewelinawoloszyn.com/mymap/d3_projection03.html How the code would look like in d3 js or how to make it work with leaflet on d3 js map?

Many thanks in advance, Neko