解决在uniapp中使用vue3 + renderjs开发的APP中用openlayers加载天地图,出现在APP中无法正常显示引用的本地图片

需求,给地图添加marker标记点,使用本地的小图标。

1. 首先,在vue3中无法使用require。

2. 直接在添加标记点的地方,使用以下四种src引入方式,都可以在h5中显示出图片,但是在APP中就无法加载了。

image: new Icon({
    // src: '../static/icons/tip_red.png',
    // src: '../../static/icons/tip_red.png',
    // src: '/src/static/icons/tip_red.png',
    src: '/static/icons/tip_red.png',
    anchor: [0.5, 1],
    scale: 0.18,
}),

3. 使用import的方式引入,在hbuilderX中编译就会报错,无论使用怎样的路径,代码举例

import tipRed from '/src/static/icons/tip_red.png'
import tipRed from '../static/icons/tip_red.png'
import tipRed from '../../static/icons/tip_red.png'
import tipRed from '/static/icons/tip_red.png'
import tipRed from 'static/icons/tip_red.png'
import tipRed from '@/static/icons/tip_red.png'
image: new Icon({
   src: tipRed,
   anchor: [0.5, 1],
   scale: 0.18,
}),

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4. 那就采用赋值的方式引入图片试试

const tipRed = '../../static/icons/tip_red.png'
const tipRed = '../static/icons/tip_red.png'
const tipRed = '/src/static/icons/tip_red.png'
const tipRed = '@/static/icons/tip_red.png'
const tipRed = '/static/icons/tip_red.png'
image: new Icon({
   src: tipRed,
   anchor: [0.5, 1],
   scale: 0.18,
}),
**以上方式在hbuilderX编译的时候并不会报错,但是在APP上仍旧无法显示图片**

我的static图片目录,是在src目录下的,而src是在根目录的。

经过验证,只有如下引用方式,可以在APP正常显示renderjs本地引用的图片

const tipRed = 'static/icons/tip_red.png'
image: new Icon({
   src: tipRed,
   anchor: [0.5, 1],
   scale: 0.18,
}),

在这里插入图片描述
在这里插入图片描述

Vue 3 + TypeScript项目使用OpenLayers,您可以按照以下步骤进行操作: 1. 安装OpenLayers使用npm或yarn安装OpenLayers库。在命令行运行以下命令: ```bash npm install ol ``` 或 ```bash yarn add ol ``` 2. 创建一个Vue组件:在您的Vue 3项目,创建一个新的Vue组件,用于显示地图。打开一个新的`.vue`文件,例如`Map.vue`。 ```vue <template> <div ref="mapContainer" class="map-container"></div> </template> <script lang="ts"> import { defineComponent, onMounted, ref } from 'vue'; import 'ol/ol.css'; import { Map, View } from 'ol'; export default defineComponent({ name: 'Map', setup() { const mapContainer = ref(null); onMounted(() => { const map = new Map({ target: mapContainer.value, view: new View({ center: [0, 0], zoom: 2, }), }); }); return { mapContainer, }; }, }); </script> <style scoped> .map-container { width: 100%; height: 400px; } </style> ``` 3. 在需要显示地图的组件使用Map组件:在需要显示地图的父组件,导入并使用Map组件。 ```vue <template> <div> <h1>My Map</h1> <Map /> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import Map from './components/Map.vue'; export default defineComponent({ name: 'App', components: { Map, }, }); </script> ``` 现在,您可以在Vue 3 + TypeScript项目使用OpenLayers显示地图。请注意,这只是一个基本示例,您可以进一步探索OpenLayers文档来了解更多功能和用法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值