threebox icon indicating copy to clipboard operation
threebox copied to clipboard

return the right object level in queryrenderedfeatures

Open joedjc opened this issue 6 years ago • 5 comments

Hi,

Firstly - this is a really great library! Thank you.

I want to use setCoords() on an object returned from QueryRenderedFeatures(). However, the object returned from QueryRenderedFeatures() doesn't contain this function/property.

The use case is for the user to drag/move a feature...so I want to use QueryRenderedFeatures to get the feature under the mouse, then setCoords to change it's position.

Thanks! Joe

joedjc avatar May 24 '19 11:05 joedjc

hi @joedjc, can you provide a test case of how you're trying to implement this?

peterqliu avatar May 24 '19 17:05 peterqliu

Hi @peterqliu thanks for the reply.

Here's the test case. I use the on click event to get a Threebox feature under the mouse. I then try to set new coords on mouse move.

I actually got it to work using a box geometry created with a 'Object3D', but it doesn't seem to work with the feature created via the loadObj method.

`var selected_feature

map.on('style.load', function() {

map
.addLayer( {
	id: 'custom_layer',
	type: 'custom',
	renderingMode: '3d',
	onAdd: function(map, mbxContext){

		window.tb = new Threebox(
			map, 
			mbxContext,
			{defaultLights: true}
		);

		// import truck from an external obj file, scaling up its size 10x
		var options = {
			obj: 'models/Truck.obj',
			mtl: 'models/Truck.mtl',
			scale: 1,
			rotation: 180
		}

		tb.loadObj(options, function(model) {

			truck = model.setCoords(origin);
			tb.add(truck);                        
		})

	},
	
	render: function(gl, matrix){
		tb.update();
	}
});

}) .on('click', function(e){ selected_feature = tb.queryRenderedFeatures(e.point)[0] }) .on('mousemove', function(e){ if ( selected_feature ) { selected_feature.object.setCoords([e.lngLat.lng, e.lngLat.lat]) } });`

joedjc avatar May 24 '19 22:05 joedjc

@peterqliu Here's an example of what i'm trying to do. This one works with the cube geometry. Click the cube once then drag and mouse to move it:

https://www.iventis.co.uk/test/Object3D.html

joedjc avatar May 27 '19 12:05 joedjc

Having dug into this I discovered I can access threebox object as it appears to be the second parent of the object returned by QueryRenderedFeatures (i.e. obj.object.parent.parent)

joedjc avatar May 29 '19 14:05 joedjc

Ah perfect. I can see how that was confusing -- will look into tweaking qRF to return the proper parent with the methods attached

peterqliu avatar May 31 '19 22:05 peterqliu