2016-10-01 6 views
0

私の主な問題は、マップ内にポイントを描画しようとすると、cxを作成するときに、それがヌルとして表示されますが、チェックすると、パッチ私の視点からは正しいです。d3.jsグローバルマップの設定点

d3.json(meteorites, function(meteorite){ 
console.log("meteorite", meteorite.features); 
svg.selectAll("circle") 
.data(meteorite.features) 
.enter() 
.append("circle") 
.attr({ 
cx: function(d){ return projection(d.geometry.coordinates[0], d.geometry.coordinates[1])[0];}, 
cy: function(d){ return projection(d.geometry.coordinates[0], d.geometry.coordinates[1])[1];}, 
r: 5, 
fill: "red" 

}); 

そして、あなたは私がここに書いたものを見ることを好む場合も、私のcodepen http://codepen.io/DiazPedroAbel/pen/bwoBZd

へのリンクは、私が投影せずにaswellしようとしているが、結果は同じですが、ヌルが、私がしなければですconsole.log()私は期待している結果を得ました。多分私はここで何かを誤解しているでしょう、私がd3で地図を描くのは初めてです。

+0

[投影] [配列を取る](https://github.com/d3/d3-3.x-api-reference/blob/master/Geo-Projections.md#_projection) 'projection([d .geometry.coordinates [0]、d.geometry.coordinates [1]]) ' – Mark

+0

私はその部分を修正しますが、それでも同じ問題があります。 – coderHook

答えて

4

geometryオブジェクトとそれに付随するgeometry.coordinatesオブジェクトを含まないJSONファイルにいくつかのエントリがあり、スクリプトが失敗するようです。私の代わりに、単純に次有するのでつまり、あなたが唯一の彼らの地理が定義座標たノードを処理していることを確認するために、.data()を取り付けるときは、基本フィルタを実装することを示唆している:

.data(meteorite.features) 

&hellipを、あなたは雑草に.filter()を使用することができますgeometryオブジェクトを持っている、またはgeometry.coordinates配列が定義されていないオブジェクトのあなたの配列からエントリアウト:

.data(meteorite.features.filter(function(d) { 
    return d.geometry && d.geometry.coordinates; 
})) 

更新され、次のコードブロックで、corrected CodePen exampleを参照してください。

d3.json(meteorites, function(meteorite){ 
    svg.selectAll("circle") 
     .data(meteorite.features.filter(function(d) { 
      return d.geometry && d.geometry.coordinates; 
     })) 
     .enter() 
     .append("circle") 
     .attr({ 
      cx: function(d){ return projection(d.geometry.coordinates)[0];}, 
      cy: function(d){ return projection(d.geometry.coordinates)[1];}, 
      r: 5, 
      fill: "red" 
    }); 
});