Leaflat迷你小地球仪

news/2024/11/2 9:33:46/

一、效果展示

在这里插入图片描述

二、代码

  • 根据leaflet-globeminimap模块化改写
  • leaflet-globeminimap.js

// L, d3, topojson
import * as L from "leaflet";
import * as d3 from "d3";
import * as topojson from "topojson";
import { wordJson } from "./world";L.Control.GlobeMiniMap = L.Control.extend({options: {position: "bottomleft",width: 82,height: 82,land: "#bbb",water: "rgba(0, 0, 0, 0.3)",marker: "#CC0000",topojsonSrc: wordJson,},//layer is the map layer to be shown in the minimapinitialize: function (options) {L.Util.setOptions(this, options);},onAdd: function (map) {this._mainMap = map;//Creating the container and stopping events from spilling through to the main map.this._container = L.DomUtil.create("div", "leaflet-control-minimap");this._container.style.width = this.options.width + "px";this._container.style.height = this.options.height + "px";L.DomEvent.disableClickPropagation(this._container);L.DomEvent.on(this._container, "mousewheel", L.DomEvent.stopPropagation);//Keep a record of this to prevent auto toggling when the user explicitly doesn't want it.this._userToggledDisplay = false;this._minimized = false;this._mainMap.on("moveend", this._onMainMapMoved, this);return this._container;},addTo: function (map) {L.Control.prototype.addTo.call(this, map);this.initCanvas();return this;},initCanvas: function () {//marker icon//https://upload.wikimedia.org/wikipedia/commons/9/93/Map_marker_font_awesome.svgd3.select(".leaflet-control-minimap").append("svg").attr("width", 82).attr("height", 82).attr("style", "position: absolute; left: 0; top: 0;").append("path").attr("d","m 768,896 q 0,106 -75,181 -75,75 -181,75 -106,0 -181,-75 -75,-75 -75,-181 0,-106 75,-181 75,-75 181,-75 106,0 181,75 75,75 75,181 z m 256,0 q 0,-109 -33,-179 L 627,-57 q -16,-33 -47.5,-52 -31.5,-19 -67.5,-19 -36,0 -67.5,19 Q 413,-90 398,-57 L 33,717 Q 0,787 0,896 q 0,212 150,362 150,150 362,150 212,0 362,-150 150,-150 150,-362 z").attr("transform", "scale(.01,-.01),translate(3600,-3900)").attr("style", "fill:" + this.options.marker);this.projection = d3.geo.orthographic().scale(40).translate([41, 41]).rotate([0, 0]).clipAngle(90);var canvas = d3.select(".leaflet-control-minimap").append("canvas").attr("width", 400).attr("height", 400);this.c = canvas.node().getContext("2d");this.path = d3.geo.path().projection(this.projection).context(this.c);var that = this;d3.json(this.options.topojsonSrc, function () {const world = wordJson;that.globe = { type: "Sphere" };that.land = topojson.feature(world, world.objects.land);});//set to current viewthis.transitionMap(this._mainMap.getCenter());},transitionMap: function (p) {var that = this;var c = that.c;var path = that.path;d3.transition().duration(1250).each("start", function () {}).tween("rotate", function () {var r = d3.interpolate(that.projection.rotate(), [-p.lng, -p.lat]);return function (t) {that.projection.rotate(r(t));c.clearRect(0, 0, that.options.width, that.options.height);c.fillStyle = that.options.water;c.beginPath();path(that.globe);c.fill();c.fillStyle = that.options.land;c.beginPath();path(that.land);c.fill();};});},onRemove: function (map) {this._mainMap.off("moveend", this._onMainMapMoved, this);this._mainMap.off("move", this._onMainMapMoving, this);},_onMainMapMoved: function (e) {if (!this._miniMapMoving) {this._mainMapMoving = true;this.transitionMap(this._mainMap.getCenter());} else {this._miniMapMoving = false;}},
});L.control.globeminimap = function (layer, options) {return new L.Control.GlobeMiniMap(layer, options);
};L.Map.mergeOptions({miniMapControl: false,
});export default L;
  • 地图引用
import L from "./leaflet-globeminimap";// 构建地图
const map = L.map(ID, {zoomControl: false,maxBounds: bounds,// 西安坐标center: [34, 108],maxZoom: 21,minZoom: 4,zoom: 4,
});// 添加了一个迷你地球仪
new L.Control.GlobeMiniMap({land: "#94cf6a",water: "#3972f7",marker: "#e44131",
}).addTo(map);


http://www.ppmy.cn/news/118639.html

相关文章

新手怎样自己DIY一套音质不错的音响?

看你预算。建议还是先玩成品箱子,知道了好的声音,才能在DIY的时候知道怎么调成好听的声音。 如果DIY的话,也是种玩法,建议如下配置:成品3寸小箱空箱;吸音棉;惠威全频3寸喇叭;接线柱…

迷你播放器

这是一个支持多种视频格式,支持多个1~9个播放窗口的播放器。 屌炸天的播放器。 下载地址:迷你播放器-C工具类资源-CSDN下载

迷你小功能之通讯录

文章目录 contact.h 类型、函数声明&#xff1b;头文件包含contact.c 功能实现mail.c 通讯录逻辑 contact.h 类型、函数声明&#xff1b;头文件包含 #pragma once #include <string.h> #include <stdio.h> #include <assert.h> #include <stdlib.h>//…

自制音响系统

这件事大概在一八年四月份就开始了&#xff0c;只不过最近才把它完成了&#xff0c;也算达到了自己满意的程度。 下面主要介绍一下它的主要组成部分。因为是拼装&#xff0c;而且原理也不复杂&#xff0c;所以就不详细介绍它的细节和制作过程了。想交流的话可以留言&#xff0c…

基于Arduino的迷你电子琴

前言 一直以来都没有系统地学过微控制器&#xff0c;使用 Arduino 时&#xff0c;也是通过现成的制作和代码进行修改&#xff0c;东一榔头西一锤&#xff0c;很多基础的东西并不了解。 前段时间&#xff0c;搜索 matlab 的教程时&#xff0c;知道了台大生机系的郭彦甫老师&am…

智能家居控制系统MECOOL KA1智能音响

MECOOL KA1是智能音箱和4K安卓电视流媒体设备的结合。该设备采用Amlogic S905Y4 SoC四核ARM Cortex -A35处理器&#xff0c;支持远场语音和免提电视控制。 MECOOL KA1通过ART&#xff08;谷歌智能语音测试&#xff09; ART 旨在验证集成了 Google 助理的设备的助理功能。要被…

Minio 入门

安装 Docker docker run --name minio \ -d --restartalways \ -p 9000:9000 \ -p 9001:9001 \ --network tool-net --network-alias minio \ -e "MINIO_ACCESS_KEYadmin" \ -e "MINIO_SECRET_KEYadmin123456" \ -v minio_data:/data \ -v min_config:/…

mifa F4户外无线蓝牙音箱 便携式迷你插卡小音响

mifa F4超薄户外蓝牙音箱&#xff0c;103g迷你小身材&#xff0c;轻松连接你的手机和智慧设备&#xff0c;可轻松进行音讯通话&#xff0c;让你时刻畅享无拘束的完美通话体验&#xff0c;支持32g APE无损音质文件的读取和播放&#xff0c;满足你对音乐的拥有感。 转载于:https:…