私はleaflet
を使用しており、json
とd3 (vers3)
を表示しています。 基本的に私はこのチュートリアルhereに従っています。d3でリーフレットにjsonを更新
これは私のコードです。最初のcallbackHandler
は、Webサイトとのユーザーの対話に基づいて、別の言語がJavaScript
に送信する情報を受け取る方法を記述しています。 pathToFile
は、d3.json(...)
によってロードされるファイル(json
)へのリンクです。
var svg = d3.select(map.getPanes().overlayPane).append("svg"),
g = svg.append("g").attr("class", "leaflet-zoom-hide");
someMethod("myName", function(pathToFile) {
console.log(pathToFile);
d3.json(pathToFile, function(error, collection) {
console.log(collection);
if (error) throw error;
// Use Leaflet to implement a D3 geometric transformation.
function projectPoint(x, y) {
var point = map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
}
var transform = d3.geo.transform({point: projectPoint}),
path = d3.geo.path().projection(transform);
var feature = g1.selectAll("path")
.data(collection.features)
.enter().append("path")
.attr("stroke-width", 0.5)
.attr("fill-opacity", 0.7)
.attr("stroke", "white")
map.on("viewreset", reset);
reset();
// Reposition the SVG to cover the features.
function reset() {
var bounds = path.bounds(collection),
topLeft = bounds[0],
bottomRight = bounds[1];
svg1 .attr("width", bottomRight[0] - topLeft[0])
.attr("height", bottomRight[1] - topLeft[1])
.style("left", topLeft[0] + "px")
.style("top", topLeft[1] + "px");
g.attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")");
feature.attr("d", path);
}
console.log("1");
});
console.log("2");
});
面白い部分は次のとおりです。初めてコードを実行したときに正常に動作します。私のjson
が地図上に表示されます。ただし、最初のcallbackHandler
(someMethod
)が2回目に実行されると(一部のユーザーがWebサイトとやりとりすると)、json
はチラシに表示されません。
は、私はマップを更新しようとした後に含まconsole.log
の出力厥:
// on startup of website, the callbackHandler "someMethod" gets
./some/path/toFile
Object {crs: Object, type: "FeatureCollection", features: Array[20]}
2
1
// after interaction with the website and execution of the callbackHandler "someMethod"
./some/other/path/toFile
Object {crs: Object, type: "FeatureCollection", features: Array[9]}
2
1
をしかし、新しいjson
は表示されません。代わりに古いものはそのままです。 なぜですか?
おかげで多くのことを入力してください。私はJSにとって非常に新しく、d3は常に私を困らせる。オプション1は動作しません。オプション2がトリックを行います。私はもう少し理解していると思います! – Stophface
オプション2では、注文を変更したいと思っています。 'g1.selectAll( "パス")()を削除; //すべてのpaths'は' VARパス= g1.selectAll( "パス")の前に来なければならない削除データ() ' – Stophface
ええ、あなたがかもしれない...しかし、それはなります。何にも影響しない。理由は、後で削除後にパスを追加しているからです。 – Cyril