8

過去 2 日間、Leaflet で非常に単純なマップを作成しようとしてきましたが、壁にぶつかっています。

以前の geoJSON ファイルから作成された 2 つのレイヤーを含む topoJSON ファイルがあります。米国の 5 つの州の郵便番号と、5 つの州のポリゴンです。

これらをリーフレットに表示したいのですが、郵便番号レイヤーのファイル サイズが小さいため、geoJSON の代わりに topoJSON を使用することが重要です。

問題は、topoJSON ファイル内の小さな状態レイヤーでさえ地図上に表示することができないことです。私は Web で多くの例を見て、Mike Bostock の例に従いました: https://github.com/mbostock/bost.ocks.org/blob/gh-pages/mike/leaflet/index.html#L131-171 .

d3だけを使用してファイルをWebブラウザに表示できるので、ファイルは問題ありません。スクリプトで topojson.feature メソッドと一緒に topoJSON の v1 を使用しています。コードは以下のとおりです。topoJSONファイルは公開できませんが、以前d3で使ったことがあるので問題ないと思います。誰かがスクリプトの異常を見つけられたら、それは素晴らしいことです。

ありがとう。

        <!DOCTYPE html>
<meta charset="utf-8">
<head> 
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://d3js.org/d3.v2.min.js?2.9.3"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<title>Gulf Zip Codes</title> 
</head>

<div id="map"></div>
<style type="text/css">
#map {
  height: 800px;
}

path {
  fill: #000;
  fill-opacity: .1;
  stroke: #fff;
  stroke-width: 1.5px;
}

path:hover {
  fill: #1f77b4;
  fill-opacity: .4;
}


</style>
<body>

<script>

var map = L.map('map').setView([32.546813, -88.374023], 6);

L.tileLayer('http://{s}.tile.cloudmade.com/1a1b06b230af4efdbb989ea99e9841af/998/256/{z}/{x}/{y}.png', {

attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

var svg = d3.select(map.getPanes().overlayPane).append("svg"),
    g = svg.append("g").attr("class", "leaflet-zoom-hide");

d3.json("states_and_codes.json", function(error, states_and_codes) {

  var bounds = d3.geo.bounds(states_and_codes);
    path = d3.geo.path().projection(project);

  var feature = g.selectAll("path")
      .data(topojson.feature(states_and_codes,      states_and_codes.objects.claim_states).features)
    .enter().append("path")
    .attr("class", "path")
    .attr("d",path);

  map.on("viewreset", reset);
  reset();

  // Reposition the SVG to cover the features.
  function reset() {
    var bottomLeft = project(bounds[0]),
        topRight = project(bounds[1]);

    svg .attr("width", topRight[0] - bottomLeft[0])
        .attr("height", bottomLeft[1] - topRight[1])
        .style("margin-left", bottomLeft[0] + "px")
        .style("margin-top", topRight[1] + "px");

    g   .attr("transform", "translate(" + -bottomLeft[0] + "," + -topRight[1] + ")");

    feature.attr("d", path);
  }

  // Use Leaflet to implement a D3 geographic projection.
  function project(x) {
    var point = map.latLngToLayerPoint(new L.LatLng(x[1], x[0]));
    return [point.x, point.y];
  } 

});



</script>
</body>
4

1 に答える 1

7

あなたがまだ探している場合、またはそこに他の人がいる場合、これは不足している部分である必要があります-TopoJsonの境界。

var bounds = d3.geo.bounds(topojson.feature(states_and_codes, states_and_codes.objects.claim_states));

ソースからの優れた TopoJson ブロックのホストもここにあります。

于 2013-07-31T10:08:30.617 に答える