# Problem to retrieve geometry instance's attribute

**URL:** <https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098>\
**Category:** CesiumJS\
**Created:** [March 26, 2014, 1:32am UTC](https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098 "2014-03-26T01:32:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Steven](https://community.cesium.com/u/Steven)\
**Post date:** [March 26, 2014, 1:32am UTC](https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098/1 "2014-03-26T01:32:32Z")

</div>

Hi all,

How do I get the value of “geometryInstance.myValue” during the mouse over event? Below is the code I have now, it doesn’t work.

var widget = new Cesium.CesiumWidget(‘cesiumContainer’);

var instances = ;

var geometryInstance = new Cesium.GeometryInstance({

geometry: new Cesium.ExtentGeometry({

extent: new Extent(

CesiumMath.toRadians(170), //west

CesiumMath.toRadians(80), //south

CesiumMath.toRadians(180), //east

CesiumMath.toRadians(90) //north

)

}),

attributes:{

color: Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1, 0, 1, 0.5))

}

});

**geometryInstance.myValue = 123** ; **// adding a value to the geometry instance.**

instances.push(geometryInstance);

widget.scene.getPrimitives().add(new Cesium.Primitive({

geometryInstances: instances,

appearance: new Cesium.PerInstanceColorAppearance(),

asynchronous: false

}));

var primitives = widget.scene.getPrimitives();

var handler = new Cesium.ScreenSpaceEventHandler(widget.scene.canvas);

handler.setInputAction(function(movement) {

var p = widget.scene.pick(movement.endPosition);

if (Cesium.defined§ && primitives.contains(p.primitive)) {

**alert(p.primitive.myValue); //How do I retrieve the “geometryInstance.myValue” here?**

}

}, Cesium.ScreenSpaceEventType.MOUSE\_MOVE);

Thanks in advance.

Steven

---

<div class="post-metadata">

**Author:** ![patrick](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/patrick/32/2045_2.png) [@patrick](https://community.cesium.com/u/patrick)\
**Post date:** [March 26, 2014, 6:49pm UTC](https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098/2 "2014-03-26T18:49:16Z")

</div>

Hi Steven,

To save memory, Primitive does not hold on to all the geometry or geometry instances once it creates WebGL resources, so it doesn’t have a reference to the geometry instance. Instead, provide an ‘id’ property (can be any type including an object) to the GeometryInstance constructor. The id is returned as part of the object from Scene.pick. See the picking section in the [tutorial](http://cesiumjs.org/2013/11/04/Geometry-and-Appearances/).

Patrick

---

<div class="post-metadata">

**Author:** ![Steven](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Steven](https://community.cesium.com/u/Steven)\
**Post date:** [March 26, 2014, 11:50pm UTC](https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098/3 "2014-03-26T23:50:45Z")

</div>

Hi Patrick,

I am using ‘id’ property for distinguishing geometry instances already, and I need to store other data with each geometry instance. Do you have any suggestion of how should I give geometry instance a value and then I can get the value when mouse over event occurred?

Thank you so much for your time.

Steven

在 2014年3月26日星期三UTC-7上午11时49分16秒，Patrick Cozzi写道：

---

<div class="post-metadata">

**Author:** ![patrick](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/patrick/32/2045_2.png) [@patrick](https://community.cesium.com/u/patrick)\
**Post date:** [March 27, 2014, 12:31pm UTC](https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098/4 "2014-03-27T12:31:29Z")

</div>

Steven,

Just make the id an object and store as many properties as you want. For example, if you had:

> id : ‘some string’

Change it to:

> id : {

> oldValue : ‘some string’,

> newProperty : ‘another string’,

> anotherProperty : 5

> }

Patrick

---

<div class="post-metadata">

**Author:** ![Steven](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Steven](https://community.cesium.com/u/Steven)\
**Post date:** [March 27, 2014, 5:44pm UTC](https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098/5 "2014-03-27T17:44:09Z")

</div>

Hi Patrick,

I used ‘id’ property as a unique index when I create geometry instances, so when I want to change the color of a geometry instances, I can use “primitive.getGeometryInstanceAttributes(id)” to get the attribute of the geometry instance with the given id. If the ‘id’ property becomes a object, how can I get the attribute of a selected geometry instance? For example:

Below is how I create geometry instances now:

for(var i=0;i\<5;i++){

instances.push(new GeometryInstance({

geometry: new ExtentGeometry({

extent: new Extent(

CesiumMath.toRadians(west), //west

CesiumMath.toRadians(south), //south

CesiumMath.toRadians(east), //east

CesiumMath.toRadians(north) //north

)

}),

attributes:{

color: ColorGeometryInstanceAttribute.fromColor(extentColor)

},

id: i

}));

}

So when I need to reset the color of a geometry instance, I can simply do this:

**var attributes = primitive.getGeometryInstanceAttributes(0); //If the ‘id’ property becomes an object, how can I get the attribute of the geometry instance?**

attributes.color = ColorGeometryInstanceAttribute.toValue(new Color(0,0,0,0));

Thanks again.

Steven

在 2014年3月27日星期四UTC-7上午5时31分29秒，Patrick Cozzi写道：

---

<div class="post-metadata">

**Author:** ![Dan\_Bagnell](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@Dan\_Bagnell](https://community.cesium.com/u/Dan_Bagnell)\
**Post date:** [March 27, 2014, 6:41pm UTC](https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098/6 "2014-03-27T18:41:42Z")

</div>

Hi Steven,

Primitive.getGeometryInstanceAttributes can take an object as an argument. If you want easy indexing, you could keep them in an array like this:

var ids = ;

for(var i=0;i\<5;i++){

var id = {

someProperty : ‘some string’,

anotherProperty : 10

};

instances.push(new GeometryInstance({

geometry: new ExtentGeometry({

extent: new Extent(

CesiumMath.toRadians(west), //west

CesiumMath.toRadians(south), //south

CesiumMath.toRadians(east), //east

CesiumMath.toRadians(north) //north

)

}),

attributes:{

color: ColorGeometryInstanceAttribute.fromColor(extentColor)

},

id: id

}));

ids.push(id);

}

Later you can do this:

var attributes = primitive.getGeometryInstanceAttributes(ids[0]);

attributes.color = ColorGeometryInstanceAttribute.toValue(new Color(0,0,0,0));

Dan

---

<div class="post-metadata">

**Author:** ![Steven](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Steven](https://community.cesium.com/u/Steven)\
**Post date:** [March 27, 2014, 9:42pm UTC](https://community.cesium.com/t/problem-to-retrieve-geometry-instances-attribute/1098/7 "2014-03-27T21:42:16Z")

</div>

Hi Patrick and Daniel,

Thanks so much, I’ve solved my problems.

Steven

在 2014年3月27日星期四UTC-7上午11时41分42秒，Daniel Bagnell写道：
