|
@@ -6,18 +6,34 @@ import {
|
|
|
VerticalOrigin,
|
|
|
when
|
|
|
} from 'cesium/Cesium'
|
|
|
+import * as Cesium from 'cesium/Cesium'
|
|
|
|
|
|
const pinBuilder = new PinBuilder()
|
|
|
|
|
|
-
|
|
|
-export const addMaker = async({ href, color = Color.ROYALBLUE, size = 48, point, icon, text, name }) => {
|
|
|
+let markerId = 0
|
|
|
+export const addMaker = async({ href, color = Color.ROYALBLUE, size = 48, point, icon, text, name, onClick }) => {
|
|
|
const loadPin = p => new Promise(resolve => when(p, canvas => resolve(canvas)))
|
|
|
+ const id = `pick_id` + markerId++
|
|
|
let image
|
|
|
-
|
|
|
+ let billboard
|
|
|
+
|
|
|
if (href) {
|
|
|
- image = await loadPin(
|
|
|
- pinBuilder.fromUrl(buildModuleUrl(href), color, size)
|
|
|
- )
|
|
|
+ const image = new Image()
|
|
|
+ await new Promise((resolve, reject) => {
|
|
|
+ image.src = href
|
|
|
+ image.onload = resolve
|
|
|
+ image.onerror = reject
|
|
|
+ })
|
|
|
+ billboard = {
|
|
|
+ width: image.width / 2,
|
|
|
+ height: image.height / 2,
|
|
|
+ pixelOffset: new Cesium.Cartesian2(0, -image.height / 4), //偏移量
|
|
|
+ image: href,
|
|
|
+ }
|
|
|
+
|
|
|
+ // image = await loadPin(
|
|
|
+ // pinBuilder.fromUrl(buildModuleUrl(href), color, size)
|
|
|
+ // )
|
|
|
} else if (icon) {
|
|
|
image = await loadPin(
|
|
|
pinBuilder.fromMakiIconId(icon, color, size)
|
|
@@ -28,9 +44,21 @@ export const addMaker = async({ href, color = Color.ROYALBLUE, size = 48, point,
|
|
|
image = pinBuilder.fromColor(color, size)
|
|
|
}
|
|
|
|
|
|
+ if (onClick) {
|
|
|
+ const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
|
|
|
+ handler.setInputAction((click) => {
|
|
|
+ const pick = viewer.scene.pick(click.position);
|
|
|
+ //选中某模型 pick选中的对象
|
|
|
+ if (pick && pick.id && pick.id.id === id) {
|
|
|
+ onClick()
|
|
|
+ }
|
|
|
+ }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
|
|
+ }
|
|
|
+
|
|
|
return viewer.entities.add({
|
|
|
+ id,
|
|
|
position: Cartesian3.fromDegrees(...point),
|
|
|
- billboard: {
|
|
|
+ billboard: billboard ? billboard : {
|
|
|
image: image.toDataURL(),
|
|
|
verticalOrigin: VerticalOrigin.BOTTOM
|
|
|
}
|
|
@@ -59,6 +87,7 @@ export const addMakers = async (args) => {
|
|
|
isArray = false
|
|
|
}
|
|
|
|
|
|
+ console.error(args)
|
|
|
let pins = await Promise.all(
|
|
|
args.map(arg => addMaker(arg))
|
|
|
)
|