2014-04-24 144 views
1

需要一些幫助...我能找到一個旋轉地球儀的例子,這很好,我甚至找到了一個方法來把紅色圓圈放在一個點上。甚至更好地設置一個計時器,一切隨着地球旋轉。但是如果我把地圖上的文字放在與紅圈相同的點上,它會在我放置它的起點處顯示,但隨着世界轉動,紅圈與地球一起移動,但是文本被凍結在點它被寫了。我試圖讓文字與世界和紅色圓圈一起旋轉。想想在美國的國家,我想輸入一個數字,當地球旋轉到中國時,巴西會有數字,數值仍然會在我放置的國家,當它旋轉美國和巴西回到前面的數字是在那裏展示。這是我的代碼,忍耐與我在一起工作時,我仍然是一個noob。感謝您的任何輸入...D3旋轉地球儀和文字

// Initialize some variables: 
var element = '#home1', 
width = $("#home1").width(), 
height = $("#home1").height(); 

var diameter = 460, 
radius = diameter/2, 
velocity = .001, 
then = Date.now(); 

var features, circles; 

var projection = d3.geo.orthographic() 
.scale(radius - 2) 
.translate([radius, radius]) 
.clipAngle(90); 


// Save the path generator for the current projection: 
var path = d3.geo.path() 
.projection(projection) 
.pointRadius(function(d,i) { 
      return radius; 
      }); 

// Define the longitude and latitude scales, which allow us to map lon/lat coordinates to pixel values: 
var lambda = d3.scale.linear() 
.domain([0, width]) 
.range([-180, 180]); 

var phi = d3.scale.linear() 
.domain([0, height]) 
.range([90, -90]); 

// Create the drawing canvas: 
var svg = d3.select("#home1").append("svg:svg") 
.attr("width", diameter) 
.attr("height", diameter); 

//Create a base circle: (could use this to color oceans) 
var backgroundCircle = svg.append("svg:circle") 
.attr('cx', diameter/2) 
.attr('cy', diameter/2) 
.attr('r', 0) 
.attr('class', 'geo-globe'); 

// Make a tag to group all our countries, which is useful for zoom purposes. (child elements belong to a 'group', which we can zoom all-at-once) 
var world = svg.append('svg:g'); 
var zoomScale = 1; // default 

// Create the element group to mark individual locations: 
var locations = svg.append('svg:g').attr('id', 'locations'); 

// Having defined the projection, update the backgroundCircle radius: 
backgroundCircle.attr('r', projection.scale()); 

// Construct our world map based on the projection: 
d3.json('world-countries.json', function(collection) { 

     features = world.selectAll('path') 
     .data(collection.features) 
     .enter() 
     .append('svg:path') 
     .attr('class', 'geo-path') 
     .attr('d', path); 

     // features.append('svg:title') 
     // .text(function(d) { return d.properties.name; }); 

    }); // end FUNCTION d3.json() 


d3.json("data.geojson", function(collection) { 
     console.log("2"); 



     cs = locations.selectAll('path') 
     .data(collection.features) 
     .enter().append('svg:path') 
     .datum(function(d) {return {type: "Point", coordinates: [d.geometry.coordinates[0], d.geometry.coordinates[1]]}; }) 
     .attr('class', 'geo-node') 
     .attr("d", path.pointRadius(5)) 
     .attr('d', path); 



     cs1 = locations.selectAll('text') 
     .data(collection.features) 
     .enter().append('svg:text') 
     .attr("transform", function(d) {return "translate(" + projection(d.geometry.coordinates) + ")"; }) 
     .attr("dy", ".35em") 
     .attr('d', path) 
     .text(function(d) { return d.properties.name; }); 

     }); // end FUNCTION d3.json() 



d3.timer(function() { 
     if(offpage === 0) 
     { 
      var angle = velocity * (Date.now() - then); 

      projection.rotate([angle,0,0]) 

      svg.selectAll("path").attr("d", path.projection(projection)); 


     } 
    }); 


d3.select(window) 
.on("touchmove", mousemove) 
.on("touchstart", mousedown); 


function mousemove() { 

    offpage = 0; 
    } 


function mousedown() { 

    offpage=1 
} 

回答

3

在你的代碼,功能(世界地圖)是一個路徑,和CS(城站)是一個路徑,但CS1(城市名)是一個文本。在您的計時器中,您將旋轉不會旋轉文本的路徑。

我的解決方案使用旋轉角度而不是角度,因此您必須修改公式。

d3.timer(function() { 
      tcounter++ 
      rotation++ 
      if (rotation>=360) rotation = 0 
      projection.rotate([rotation,0,0]) 
      www.attr("d", path.projection(projection)); 
      citydot.attr("d", path.projection(projection)); 
      ctext.attr("transform", function(d) { 
        return "translate(" + projection(d.geometry.coordinates) + ")"; }) 
       .text(function(d) { 
       if (((rotation + d.geometry.coordinates[0] > -90) && (rotation + d.geometry.coordinates[0] <90)) || 
        ((rotation + d.geometry.coordinates[0] > 270) && (rotation + d.geometry.coordinates[0] <450))) 
        return d.properties.city; 
        else return "" }); 

      if (tcounter > 360) return true 
       else return false 
     })