Created
October 11, 2017 16:33
-
-
Save green3g/2f04a6e9513843b51c35e48adeb9f7c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
identify (event) { | |
this.view.popup.features = []; | |
// map existing popup promises to es6 promises, so we can use promise.all | |
const promises = this.view.popup.promises.map((p) => { | |
return new Promise((resolve) => { | |
p.then(resolve); | |
}); | |
}); | |
// for each layer, idenitify it, and push a promise | |
this.view.map.layers.forEach((layer) => { | |
if (!layer.visible) { | |
return; | |
} | |
if (methods.hasOwnProperty(layer.declaredClass)) { | |
// get a promise that should resolve to some identify response | |
const _promise = methods[layer.declaredClass](event, this.view, layer, this.layerInfos); | |
const promise = new Promise(resolve => { | |
_promise.then((response) => { | |
resolve({ | |
response: response, | |
layerId: layer.id | |
}); | |
}); | |
}); | |
promises.push(promise); | |
} else { | |
dev.warn(`no identify function registered for type ${layer.declaredClass}`); | |
} | |
}); | |
// after all promises resolve, update the popup | |
Promise.all(promises).then((data) => { | |
console.log(data); | |
// assign popup templates to each | |
// data.forEach(this.assignPopupTemplate.bind(this)); | |
// reduce and sort to a plain array of features | |
const identifiedFeatures = data.reduce((a, b) => { | |
// assignPopupTemplate assigns a default popup template | |
return a.concat(this.assignPopupTemplate(b)); | |
}, []).sort((a, b) => { | |
// sort all the features according to distance from map click (closest features first) | |
const geoms = [a, b].map((f) => { | |
return f.geometry.extent ? f.geometry.extent.center : f.geometry; | |
}); | |
const distances = geoms.map((geom, index) => { | |
return geometryEngine.distance(event.mapPoint, geoms[index], 'feet'); | |
}); | |
const ret = distances[0] < distances[1] ? -1 : distances[0] > distances[1] ? 1 : 0; | |
return ret; | |
}); | |
// concat with existing features in the popup | |
const features = this.view.popup.features.concat(identifiedFeatures); | |
// open the popup with the given features | |
this.view.popup.open({ | |
updateLocationEnabled: true, | |
features: features, | |
visible: true, | |
selectedFeatureIndex: 0 | |
}); | |
}); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment