赞
踩
- const express = require("express");
- const path = require("path");
- const os = require("os");
-
- const app = express();
-
- // 允许所有来源的跨域请求
- app.use((req, res, next) => {
- res.header("Access-Control-Allow-Origin", "*");
- res.header(
- "Access-Control-Allow-Headers",
- "Origin, X-Requested-With, Content-Type, Accept"
- );
- next();
- });
-
- // // 设置瓦片文件夹的路径
- // const tilesFolder = path.join(__dirname, 'MAP');
-
- // 配置卫星图层的路由
- app.use("/satellite", express.static(path.join(__dirname, "MAP/satellite")));
-
- // 配置城市图层的路由
- app.use("/city", express.static(path.join(__dirname, "MAP/city")));
-
- // 获取当前操作系统是否为 Linux
- const isLinux = process.platform === "linux";
-
- // 根据不同操作系统设置基础 URL
- let base_url;
-
- if (isLinux) {
- function getIPByInterface(interfaceName) {
- const interfaces = os.networkInterfaces();
- if (interfaces.hasOwnProperty(interfaceName)) {
- const interfaceInfo = interfaces[interfaceName];
- for (const iface of interfaceInfo) {
- // 只获取 IPv4 地址,并且不是 loopback 地址
- if (iface.family === "IPv4" && !iface.internal) {
- return iface.address;
- }
- }
- }
- return null; // 如果指定的接口不存在或者没有对应的 IP 地址,则返回 null
- }
-
- const interfaceName = "eth1"; // 指定要检查的网络接口名称
- const ip = getIPByInterface(interfaceName);
- if (ip) {
- console.log(`IP address of ${interfaceName}: ${ip}`);
- base_url = `http://${ip}:3000`;
- } else {
- console.log(`No IP address found for interface ${interfaceName}`);
- }
- } else {
- const port = process.env.PORT || 3000;
- const host = process.env.HOST || "localhost";
- // 其他操作系统下的默认设置
- base_url = `http://${host}:${port}`;
- }
-
- // 启动服务器
- const server = app.listen(3000, () => {
- console.log(`Server is running on ${base_url}`);
- });
- const A_MAP_KEY = ""; // 地图key
- const A_MAP_SECRET_KEY = ""; // 地图密钥
- // 地图初始化
- const script = document.createElement('script');
- script.src = `https://webapi.amap.com/maps?v=1.4.15&key=${A_MAP_KEY}`;
- script.onload = this.initAmap.bind(this);
- document.head.appendChild(script);
- const script1 = document.createElement('script');
- script1.src = '../../map.js';
- script1.onload = this.initAmap.bind(this);
- document.head.appendChild(script1);
- //初始化地图
- initAmap() {
- // todo 这里的baseurl 是瓦片地图的图层 根据服务器来
- let base_url = "http://localhost:3000";
-
- const customLayer = [new AMap.TileLayer({
- getTileUrl: function (x, y, z) {
- return `${base_url}/city/${z}/${x}/${y}.jpg`;
- },
- opacity: 1,
- zIndex: 99,
- })]
- this.map = new AMap.Map("container", {
- zoom: 12,
- resizeEnable: true,
- defaultCursor: 'pointer',
- showLabel: true, //是否显示文字标注
- center: this.centerPoint ,
- scrollWheel: true,
- viewMode: '3D', //显示罗盘 就需要3D地图了
- pitch: 0,
- layers: customLayer,
- });
- this.map.add(customLayer)
- AMap.plugin(
- [
- "AMap.ToolBar",
- "AMap.Scale",
- 'AMap.ControlBar'
- // "AMap.MapType",
- ],
- );
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。