2016-08-29 1 views
1

이없이 SVG 높이에 맞게하면 기능을이다스케일 경로 블라인드 시행 착오

var singaporeJSON = {"type":"FeatureCollection","geocoding":{"creation_date":"2016-01-09","generator":{"author":{"name":"Mapzen"},"package":"fences-builder","version":"0.1.2"},"license":"ODbL (see http://www.openstreetmap.org/copyright)"},"features":[{"properties":{"name:display":"Singapore","name":"singapore"},"geometry":{"type":"MultiPolygon","coordinates":[[[[104.5706735,1.4419380999999996],[104.55258289999996,1.4106225999999995],[104.3891305,1.3172011999999995],[104.3488465,1.3331733],[104.35531379999998,1.3563739000000001],[104.37827119999996,1.4102812999999996],[104.4633395,1.4991501],[104.48455620000003,1.5130449],[104.486389,1.5123502],[104.4986883,1.5064215999999995],[104.52192920000003,1.4921171000000004],[104.5327622,1.4838079999999998],[104.5430018,1.4747782999999997],[104.5526002,1.4650701999999998],[104.5706735,1.4419380999999996]]],[[[104.12611109999997,1.28925],[104.12517559999996,1.2758195999999995],[104.11490400000002,1.2765354],[104.0927257,1.2733869],[104.0333333,1.2694999999999996],[103.9184908,1.2226474],[103.88074999999998,1.20725],[103.85983329999998,1.1959722],[103.80499999999999,1.1714444],[103.74069440000004,1.1303611],[103.67072219999996,1.1794444],[103.66069440000003,1.1881667],[103.57233329999997,1.1987500000000004],[103.56666670000003,1.1955],[103.60286109999997,1.2641666999999996],[103.6170833,1.3154166999999997],[103.6178333,1.3216111],[103.6300556,1.3410556],[103.64919440000003,1.3799166999999997],[103.65297219999995,1.3870833],[103.6535,1.3912778000000001],[103.6571667,1.4004166999999996],[103.6639167,1.4104721999999998],[103.6694444,1.4157499999999996],[103.67388889999995,1.4281389],[103.68333329999997,1.43725],[103.69405560000003,1.4398889],[103.69886109999997,1.4433055999999995],[103.70375,1.4507778],[103.71411110000003,1.4574721999999998],[103.72833329999997,1.4601389],[103.74677779999998,1.4503889],[103.76069440000003,1.4483889],[103.7711111,1.4527778],[103.7904722,1.4651389],[103.80366670000002,1.4765],[103.81266670000002,1.4784722],[103.8221667,1.4766943999999993],[103.83416670000003,1.4729999999999996],[103.85199999999999,1.4661666999999996],[103.8586667,1.4629443999999996],[103.8649722,1.4588610999999996],[103.8681667,1.4565278],[103.88613889999996,1.4351388999999997],[103.8978611,1.4279443999999992],[103.91275,1.4275277999999996],[103.91683330000002,1.4266666999999993],[103.93341670000002,1.4304166999999997],[103.93769440000005,1.4304721999999999],[103.94252780000002,1.4278332999999992],[103.96066670000003,1.42475],[103.96680560000003,1.4221666999999996],[103.9835833,1.4242778],[103.98616670000003,1.4249166999999996],[103.99525000000003,1.4236943999999991],[104.00286109999998,1.4206111],[104.0233056,1.4391388999999997],[104.0408333,1.4438889],[104.05747219999996,1.4398610999999992],[104.07119440000002,1.4346389],[104.07647219999996,1.4309166999999996],[104.08847219999996,1.4175832999999995],[104.09075,1.4124443999999996],[104.09266670000002,1.4059443999999992],[104.0930278,1.3998055999999999],[104.09247219999996,1.3942500000000004],[104.0835833,1.3687500000000001],[104.07972219999996,1.3575],[104.08527779999997,1.3466666999999997],[104.12611109999997,1.28925]]]]},"type":"Feature"}]} 

이 코드

var w = $('#map').parent().width(); 
      var h = $(document).height() - 100; 

      var projection = d3.geoEquirectangular() 
           .scale(w)//scale it down to h/(2*Math.PI) 
           .translate([-w , h]);//translate as per your choice 


      //Create SVG element 
      var svg = d3.select("#map") 
         .append("svg") 
         .attr("width", w) 
         .attr("height", h); 


      svg.selectAll("path") 
      .data(singaporeJSON.features) 
      .enter() 
      .append("path") 
      .attr("d", d3.geoPath().projection(projection))    
      .style("fill", "steelblue"); 

되고 결과가있다 (참조 첨부, 빨간색 원에 의해 강조) The red circle is the drawn path. SVG 높이와 너비에 맞게 시행 착오없이 크기를 조정하려면 어떻게합니까?

+1

확인 될이 : http://stackoverflow.com/questions/14492284/center-a-map-in-d3-given-a-geojson-object –

답변

1

대답은 링크에 있습니다. Gerado Futado가 언급 한 .

이 최종 코드

// Create a unit projection. 
var projection = d3.geo.albers() 
    .scale(1) 
    .translate([0, 0]); 

// Create a path generator. 
var path = d3.geo.path() 
    .projection(projection); 

// Compute the bounds of a feature of interest, then derive scale & translate. 
var b = path.bounds(feature), // if u have features, use features[0] 
    s = .95/Math.max((b[1][0] - b[0][0])/width, (b[1][1] - b[0][1])/height), 
    t = [(width - s * (b[1][0] + b[0][0]))/2, (height - s * (b[1][1] + b[0][1]))/2]; 

// Update the projection to use computed scale & translate. 
projection 
    .scale(s) 
    .translate(t); 

svg.selectAll("path") 
      .data(singaporeJSON.features) 
      .enter() 
      .append("path") 
      .attr("d", d3.geoPath().projection(projection))      .style("fill", "steelblue");