最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - d3.js Add a circle in d3.geo.path - Stack Overflow

programmeradmin1浏览0评论

I have created a map from a mbtile converted to a geojson, the projection is WGS84. I load it like that :

var map = svg.append("g").attr("class", "map");
var path = d3.geo.path().projection(d3.geo.albers().origin([3.4,46.8]).scale(12000).translate([590, 570]));
    d3.json('myjsonfile.json', function(json) {
        map.selectAll('path').data(json.features).enter().append('path').attr('d', path)
});

Now I would like to add a svg element (a dot, a circle, a point (i don't know)) with its (lat,lng) coordinate in my svg.

I can't figure how to do that.

I have created a map from a mbtile converted to a geojson, the projection is WGS84. I load it like that :

var map = svg.append("g").attr("class", "map");
var path = d3.geo.path().projection(d3.geo.albers().origin([3.4,46.8]).scale(12000).translate([590, 570]));
    d3.json('myjsonfile.json', function(json) {
        map.selectAll('path').data(json.features).enter().append('path').attr('d', path)
});

Now I would like to add a svg element (a dot, a circle, a point (i don't know)) with its (lat,lng) coordinate in my svg.

I can't figure how to do that.

Share Improve this question edited May 3, 2012 at 19:33 ZachB 15.4k5 gold badges65 silver badges93 bronze badges asked Apr 21, 2012 at 18:57 Laurent DebriconLaurent Debricon 4,5172 gold badges26 silver badges26 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 19

You need to separate out the projection so you can use it again to project the lat/lon of your point:

var map = svg.append("g").attr("class", "map");
var projection = d3.geo.albers()
    .origin([3.4,46.8])
    .scale(12000)
    .translate([590, 570]);
var path = d3.geo.path().projection(projection);
d3.json('myjsonfile.json', function(json) {
    map.selectAll('path')
        .data(json.features)
      .enter().append('path').attr('d', path);
    // now use the projection to project your coords
    var coordinates = projection([mylon, mylat]);
    map.append('svg:circle')
        .attr('cx', coordinates[0])
        .attr('cy', coordinates[1])
        .attr('r', 5);
});

Another way to do this is just to translate the dot by the projected coords:

map.append('svg:circle')
    .attr("transform", function(d) { 
        return "translate(" + projection(d.coordinates) + ")"; 
    })
    .attr('r', 5);
发布评论

评论列表(0)

  1. 暂无评论