Category Archives: JavaScript

Cesiumjs – How to access the coordinates

phpmind-cesiumjs-access-the-coordinates

 var entities = dataSource.entities.values; 
                console.log("entities_positions"); 
                for (var i = 0; i < entities.length; i++) { 
                    entity =  entities[i]; 
                      var cartesianPositions = entity.polyline.positions; 
                      var cartographicPositions = viewer.scene.globe.ellipsoid.cartesianArrayToCartographicArray(cartesianPositions._value); 
                      var longitude = cartographicPositions[0].longitude; 
                      var latitude = cartographicPositions[0].latitude; 
                      var height = cartographicPositions[0].height; 
                      console.log(longitude); 
                      console.log(latitude); 

                } 

Those coordinates are given in radians. You can convert them back to degrees with Cesium.Math.toDegrees(radianValue).

Share

Cesiumjs – Show and hide multiple entity/labels

How can i toggle multiple labels/entities ?

phpmind-cesiumjs-show-hide

var viewer = new Cesium.Viewer('cesiumContainer', { infoBox : false });
var entities = viewer.entities;

var labels = entities.add(new Cesium.Entity());

var longitude = - 106;
for (var i = 0; i < 5; ++i) {
    entities.add({
        parent : labels,
        position : Cesium.Cartesian3.fromDegrees(longitude, 45.0),
        label : {
            text : i.toString()
        }
    });
    longitude++;
}

viewer.zoomTo(viewer.entities);

Sandcastle.addToolbarButton('Toggle Labels', function(){
    labels.show = !labels.show;
});
Share

cesiumjs – How to Make Multicolor Polyline

How does segmenting a polyline into different colors work.

Cesiumjs-Multicolor-Polyline

var viewer = new Cesium.Viewer('cesiumContainer');

var primitive = new Cesium.Primitive({
  geometryInstances : new Cesium.GeometryInstance({
    geometry : new Cesium.PolylineGeometry({
      positions : [
        Cesium.Cartesian3.fromElements(-2822918.76, -1347627.64, 5539750.33),
        Cesium.Cartesian3.fromElements(-2860091, -1790883.7, 6104936.09),
        Cesium.Cartesian3.fromElements(-2869994.31, -2212991.67, 6598178.01)
      ],
      width : 2.0,
      vertexFormat : Cesium.PolylineColorAppearance.VERTEX_FORMAT,
      colors: [Cesium.Color.RED, Cesium.Color.BLUE],
      colorsPerVertex: false
    })
  }),
  appearance : new Cesium.PolylineColorAppearance({
    translucent : false
  })
});

viewer.scene.primitives.add(primitive);
Share

How to add days in a date string using JavaScript ?

This is how you can add number of days in a date string using JavaScript.
In this example i have added 6 days in a date. Please check the demo.

	var someDate = new Date("2014-06-20");
	var numberOfDaysToAdd = 6; // add 6 days
	someDate.setDate(someDate.getDate() + numberOfDaysToAdd);

	var dd = someDate.getDate();
	var mm = someDate.getMonth() + 1;
	var y = someDate.getFullYear();

	var someFormattedDate = y +'-'+ mm +'-'+dd;
// output 2014-06-25

Another example using php.
You can add any number of days if you want to reduce number of days use minus(-) sign instead.


Demo

Share

JQuery – how to get a key and value with autocomplete?

Jquery autocomplet is easy but when you have to get the key of the array and store into the database or just you want to get it’s a bit tricky.
I have used the jquery UI autocomplete feature with JSON object which you can create with given PHP code.
As soon as you choose a list of the values, key automatically gets populated into another hidden field. For sake of understanding I made it text field.
You can do whatever you want with that id!!







Share

How to pass dropdown value to text box ?

I had a situation where requirement was to pass select box value to another text field. Using jQuery it is very easy.

See the code below.








Share

How to Show hide content using core JavaScript ?

Today I was working on another website and had very long text/content and thought to use Jquery, there are several examples given on internet. I tried to use some of them of course they are very simple but because of some reason it didn’t work for me either in IE and chrome.
So decided to compile my own show hide JavaScript which is based on DOM!
Here is very simple code and it works everywhere, Firefox, Internet Explorer, Chrome, Safari etc.

Steps are self explanatory.
A. Use JavaScript code
B. Use Css Class
C. See the example and compile your own!




This is first paragraph of my big content. Click to See more.

Here is your hidden text which you want to hide!! Copy your text here!

Click to hide this content.

Share

What is difference between setInterval () and setTimeout () ?

setTimeout execute javascript statement AFTER x interval.
For example – setTimeout(“execute_something();”, 2000);
This is execute function execute_something() after 2 seconds.

setInterval execute javascript statement EVERY x interval.
For example – setInterval(“execute_something();”, 2000);
This is execute function execute_something() in every 2 seconds.

Share