高德地图报错 Uncaught TypeError: AMap.Geocoder is not a constructor

本文介绍了解决在谷歌等浏览器中地图定位出现的问题,并提供了一种通过加载AMap.Geocoder插件来修正定位精度的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原本我是这样写的,但是在谷歌或者其他浏览器的时候就报错了,而且谷歌等浏览器,pc端是只能定位到附近的基站

 

出现这个报错可以添加  AMap.plugin("AMap.Geocoder",function(){})

Map.vue:83 初始化主地图失败: TypeError: Cesium.WorldTerrain is not a constructor at initMainMap (Map.vue:50:24) at Map.vue:199:3 at runtime-core.esm-bundler.js:2836:40 at callWithErrorHandling (runtime-core.esm-bundler.js:199:19) at callWithAsyncErrorHandling (runtime-core.esm-bundler.js:206:17) at hook.__weh.hook.__weh (runtime-core.esm-bundler.js:2816:19) at flushPostFlushCbs (runtime-core.esm-bundler.js:385:28) at render2 (runtime-core.esm-bundler.js:6048:7) at mount (runtime-core.esm-bundler.js:3962:13) at app.mount (runtime-dom.esm-bundler.js:1775:19) initMainMap @ Map.vue:83 (匿名) @ Map.vue:199 (匿名) @ runtime-core.esm-bundler.js:2836 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 hook.__weh.hook.__weh @ runtime-core.esm-bundler.js:2816 flushPostFlushCbs @ runtime-core.esm-bundler.js:385 render2 @ runtime-core.esm-bundler.js:6048 mount @ runtime-core.esm-bundler.js:3962 app.mount @ runtime-dom.esm-bundler.js:1775 (匿名) @ main.ts:8 Map.vue:132 初始化鹰眼地图失败: TypeError: Cesium.RectanglePrimitive is not a constructor at initOverviewMap (Map.vue:122:32) at Map.vue:200:3 at runtime-core.esm-bundler.js:2836:40 at callWithErrorHandling (runtime-core.esm-bundler.js:199:19) at callWithAsyncErrorHandling (runtime-core.esm-bundler.js:206:17) at hook.__weh.hook.__weh (runtime-core.esm-bundler.js:2816:19) at flushPostFlushCbs (runtime-core.esm-bundler.js:385:28) at render2 (runtime-core.esm-bundler.js:6048:7) at mount (runtime-core.esm-bundler.js:3962:13) at app.mount (runtime-dom.esm-bundler.js:1775:19) initOverviewMap @ Map.vue:132 (匿名) @ Map.vue:200 (匿名) @ runtime-core.esm-bundler.js:2836 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 hook.__weh.hook.__weh @ runtime-core.esm-bundler.js:2816 flushPostFlushCbs @ runtime-core.esm-bundler.js:385 render2 @ runtime-core.esm-bundler.js:6048 mount @ runtime-core.esm-bundler.js:3962 app.mount @ runtime-dom.esm-bundler.js:1775 (匿名) @ main.ts:8 localhost/:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON at JSON.parse (<anonymous>) at Resource.js:1180:17 (匿名) @ Resource.js:1180 localhost/:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON at JSON.parse (<anonymous>) at Resource.js:1180:17 如上所示报错,修改下面代码:<template> <div class="map-container"> <!-- 主地图容器 --> <div id="cesiumContainer" class="main-map"></div> <!-- 鹰眼地图容器 --> <div id="overviewMap" class="overview-map"></div> </div> </template> <script setup lang="ts"> import { onMounted, onUnmounted, ref } from 'vue'; import * as Cesium from 'cesium'; import 'cesium/Build/Cesium/Widgets/widgets.css'; // 株洲市范围常量 const ZHUZHOU_EXTENT = new Cesium.Rectangle( Cesium.Math.toRadians(112.5), // west Cesium.Math.toRadians(26.0), // south Cesium.Math.toRadians(114.5), // east Cesium.Math.toRadians(28.0) // north ); // 地图实例引用 const mainViewer = ref<Cesium.Viewer | null>(null); const overviewViewer = ref<Cesium.Viewer | null>(null); const rectanglePrimitive = ref<Cesium.RectanglePrimitive | null>(null); // 初始化主地图 const initMainMap = async () => { try { // 设置Cesium访问令牌 Cesium.Ion.defaultAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; // 创建主地图实例 mainViewer.value = new Cesium.Viewer('cesiumContainer', { sceneMode: Cesium.SceneMode.SCENE3D, animation: false, baseLayerPicker: false, fullscreenButton: true, geocoder: false, homeButton: true, infoBox: false, sceneModePicker: true, selectionIndicator: true, timeline: false, navigationHelpButton: false, shouldAnimate: true, // 修复地形创建方式 terrainProvider: new Cesium.WorldTerrain({ requestVertexNormals: true, requestWaterMask: true }), imageryProvider: new Cesium.UrlTemplateImageryProvider({ url: "http://124.232.190.30:9000/proxy/pk1725866655224/map/zzzsyx_18/{z}/{x}/{y}.png", minimumLevel: 1, maximumLevel: 17 }) }); // 设置初始视图到株洲市范围 mainViewer.value.camera.flyTo({ destination: ZHUZHOU_EXTENT, orientation: { heading: Cesium.Math.toRadians(0), pitch: Cesium.Math.toRadians(-30), roll: 0.0 }, duration: 2.0 }); // 添加错误处理 mainViewer.value.scene.globe.tileLoadProgressEvent.addEventListener((pending) => { if (pending === 0) { console.log('所有瓦片加载完成'); } }); mainViewer.value.scene.error.addEventListener((error) => { console.error('Cesium错误:', error); }); } catch (error) { console.error('初始化主地图失败:', error); } }; // 初始化鹰眼地图 const initOverviewMap = () => { try { // 创建鹰眼地图实例 overviewViewer.value = new Cesium.Viewer('overviewMap', { sceneMode: Cesium.SceneMode.SCENE2D, baseLayerPicker: false, fullscreenButton: false, geocoder: false, homeButton: false, infoBox: false, sceneModePicker: false, selectionIndicator: false, timeline: false, navigationHelpButton: false, animation: false, imageryProvider: new Cesium.ArcGisMapServerImageryProvider({ url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer' }) }); // 隐藏不需要的控件 overviewViewer.value.cesiumWidget.creditContainer.style.display = "none"; // 设置鹰眼地图范围 overviewViewer.value.camera.setView({ destination: new Cesium.Rectangle( Cesium.Math.toRadians(110.0), // west Cesium.Math.toRadians(24.0), // south Cesium.Math.toRadians(116.0), // east Cesium.Math.toRadians(30.0) // north ) }); // 添加株洲市范围指示器 rectanglePrimitive.value = new Cesium.RectanglePrimitive({ rectangle: ZHUZHOU_EXTENT, material: Cesium.Color.RED.withAlpha(0.3), outline: true, outlineColor: Cesium.Color.RED, outlineWidth: 2 }); overviewViewer.value.scene.primitives.add(rectanglePrimitive.value); } catch (error) { console.error('初始化鹰眼地图失败:', error); } }; // 同步主地图和鹰眼地图 const syncMaps = () => { try { if (!mainViewer.value || !overviewViewer.value || !rectanglePrimitive.value) return; // 监听主地图相机变化 mainViewer.value.camera.changed.addEventListener(() => { const camera = mainViewer.value!.camera; // 计算当前视图范围 const rectangle = camera.computeViewRectangle(); if (rectangle) { // 更新鹰眼地图中的矩形 rectanglePrimitive.value!.rectangle = rectangle; // 更新鹰眼地图视角(如果矩形超出当前视图) const overviewCamera = overviewViewer.value!.camera; const overviewRectangle = overviewCamera.computeViewRectangle(); if (overviewRectangle && !Cesium.Rectangle.contains(overviewRectangle, rectangle)) { // 扩展矩形以包含当前视图 const expandedRect = new Cesium.Rectangle( Math.min(overviewRectangle.west, rectangle.west), Math.min(overviewRectangle.south, rectangle.south), Math.max(overviewRectangle.east, rectangle.east), Math.max(overviewRectangle.north, rectangle.north) ); overviewCamera.flyTo({ destination: expandedRect, duration: 0.5 }); } } }); // 鹰眼地图点击事件 overviewViewer.value.screenSpaceEventHandler.setInputAction((click: any) => { const position = click.position; const ray = overviewViewer.value!.camera.getPickRay(position); const cartesian = overviewViewer.value!.scene.globe.pick(ray, overviewViewer.value!.scene); if (cartesian) { // 主地图跳转到点击位置 mainViewer.value!.camera.flyTo({ destination: cartesian, orientation: { heading: mainViewer.value!.camera.heading, pitch: mainViewer.value!.camera.pitch, roll: 0.0 }, duration: 1.0 }); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); } catch (error) { console.error('地图同步失败:', error); } }; // 组件挂载时初始化地图 onMounted(() => { initMainMap(); initOverviewMap(); // 等待地图初始化完成后再同步 setTimeout(syncMaps, 1000); }); // 组件卸载时销毁地图实例 onUnmounted(() => { try { if (mainViewer.value) { mainViewer.value.destroy(); mainViewer.value = null; } if (overviewViewer.value) { overviewViewer.value.destroy(); overviewViewer.value = null; } } catch (error) { console.error('销毁地图实例失败:', error); } }); </script> <style> /* 全局重置样式 */ * { margin: 0; padding: 0; box-sizing: border-box; } html, body { width: 100%; height: 100%; overflow: hidden; /* 防止滚动条 */ font-family: sans-serif; } </style> <style scoped> .map-container { position: fixed; /* 使用 fixed 定位 */ top: 0; left: 0; width: 100%; height: 100%; /* 使用 100% 高度 */ overflow: hidden; /* 隐藏溢出内容 */ background-color: #1e1e1e; /* 深色背景防止白色间隙 */ } .main-map { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; } .overview-map { position: absolute; bottom: 20px; right: 20px; width: 300px; height: 200px; border: 2px solid #fff; box-shadow: 0 0 15px rgba(0, 0, 0, 0.7); z-index: 100; background-color: #000; /* 添加背景色 */ border-radius: 4px; /* 圆角 */ } /* 响应式调整 */ @media (max-width: 768px) { .overview-map { width: 150px; height: 100px; bottom: 10px; right: 10px; } } </style> 同时如图所示,顶部菜单栏的样式存在问题,蓝色部分太长了,都到了下面了,基于此修改代码发我
最新发布
07-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值