I need some assistance using javascript to find my pin entity

I can’t seem to get the entities.getById to work, it always comes up as undefined.

Here’s how I create the pin entity:

// add a map pin
var pinBuilder = new Cesium.PinBuilder();
var url = Cesium.buildModuleUrl(‘Assets/Textures/maki/camera.png’);
var groceryPin = Cesium.when(pinBuilder.fromUrl(url, Cesium.Color.GREEN, 48), function (canvas) {
return viewer.entities.add({
name: ‘Photo’,
position: Cesium.Cartesian3.fromDegrees(-116.52, 35.02),
billboard: {
image: canvas.toDataURL(),
verticalOrigin: Cesium.VerticalOrigin.BOTTOM
},
description: ‘

This is some text


});
});
groceryPin = viewer.entities.getById(“Photo”); ** // this always comes up undefined!!!**

There are 2 things I would like to do once I get a handle on this entity.

1.) I would like to get the position of groceryPin

2.) I’d like to open/load and close the groceryPins infoBox.

Here’s how I tried (and failed) to get the pins position (so I hardcoded the position into the event handler):

// add prox detection
var detectionRange = 1000;
function checkPosition() {
var vehicleEntity = findEntityById(“Vehicle”);
if (vehicleEntity != undefined){
var currentPosition = vehicleEntity.position.getValue(viewer.clock.currentTime);
var photoEntity = viewer.entities.getById(“Photo”)
var pinPosition = Cesium.Cartesian3.fromDegrees(-116.52, 35.02); //photoEntity.position.getValue(viewer.clock.currentTime)
var distanceFromPin = Cesium.Cartesian3.distance(currentPosition,
pinPosition);
if (distanceFromPin < detectionRange) {
console.log(“distance from pin:” + distanceFromPin);
viewer.selectedEntity = photoEntity; ** // this was suppose to open the pins infoBox and it might work if I can “find” the pin entity**
} else { viewer.selectedEntity = undefined;}
}
}

function findEntityById(entityId) {
for (idx = 0; idx < viewer.dataSources.length; idx++)
{
var datasource = viewer.dataSources.get(idx);
return datasource.entities.getById(entityId);
};
return undefined;
}
var listener = viewer.scene.preRender.addEventListener(checkPosition);

I’m very new to this package so, I’m sure there’s something I just don’t understand or have overlooked.

All suggestions and comments are welcome.

-Maynard

You probably should use the id property, like:
return viewer.entities.add({
id: ‘Photo’,

``

Good luck, Willem

Hi Maynard,

The Enitity name and id fields are separate. It looks like you’re setting name but searching by id, which may be the issue.

Also in the future, just for your convenience, you will find it easier to share code snippets via Sandcastle: http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Hello%20World.html&label=Showcases

Here are some instructions: http://cesiumjs.org/tutorials/Sandcastle-Tutorial/

Hope that helps,

  • Rachel

thank you for your suggestions, now that you point out that id and name are two different attributes it makes sense.

-Maynard