Plot points in map d3 javascript

后端 未结 1 1384
北荒
北荒 2021-01-24 21:43

i want to draw on map base on longitude and latitude in csv file called tree.csv on a map that i using an image . My csv file include many lines ,so i will just put some lines

相关标签:
1条回答
  • 2021-01-24 22:10

    Your problem lies in that you aren't providing coordinates to be projected.

    A d3 geoProjection takes a longitude latitude pair and projects it to an x,y svg coordinate (a projection returns a coordinate as: [x,y], which is why you use this form in your code: projection(coord)[0] to get the cx value). You are seeking to project only a longitude, and then only a latitude:

    .attr("cx",function(d) { return projection(d["Longitude"])[0];})
    .attr("cy",function(d) { return projection(d["Latitude"])[1];})
    

    In this case, projection won't return an svg coordinate as you aren't providing a geographical coordinate to project. You need to project both longitude and latitude, becuase x and y values produced in a projection are usually (not always) co-dependent - in any conical projection for example, the output y (or x) value is dependent on both latitude and longitude. Further, as projection() returns [x,y], it requires both longitude and latitude for every projection.

    Instead try:

    .attr("cx",function(d) { return projection([d["Longitude"],d["Latitude"]])[0];})
    .attr("cy",function(d) { return projection([d["Longitude"],d["Latitude"]])[1];})
    

    Remeber that d3 geoprojections expect the form: projection([longitude, latitude]), changing the order of longitude and latitude will produce unexpected results.

    var data = [
    {longitude:1,latitude:1},
    {longitude:-1,latitude:1},
    {longitude:1,latitude:-1},
    {longitude:-1,latitude:-1}
    ]
    
    var svg = d3.select("body")
       .append("svg")
       .attr("width",200)
       .attr("height",200);
       
    var projection = d3.geoMercator()
      .translate([100,100]);
      
    var circles = svg.selectAll("circle")
      .data(data)
      .enter()
      .append("circle")
      .attr("cx",function(d) { return projection([d.longitude,d.latitude])[0];
       })
      .attr("cy",function(d) { return projection([d["longitude"],d["latitude"]])[1];
       })
       .attr("r",2)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>

    0 讨论(0)
提交回复
热议问题