Process self-adaption solution method based on Gade map
By verifying the version relationship and dependency loading status, the process adaptive loading of Amap JSAPI is realized, which solves the problem of version confusion and repeated loading. It supports ESModule modularization and preloading, improving usage efficiency and performance.
Patent Information
- Application Number
- CN202410682144.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-29
- Publication Date
- 2024-09-17
AI Technical Summary
The existing Amap JSAPI loading method does not support version control, systematic reuse, ESmodule modularization, preloading and on-demand loading, leading to problems such as version confusion, repeated loading, and first screen blocking.
By checking the version relationship of AMap, AMapUI and Loca, we can determine the situation of repeated loading and dependent loading, use JS code to implement process-adaptive loading logic, use NodeJS and esbuild tools to package into npm packages, support ESModule modularization, and use the browser's The prefetch mechanism performs preloading and captures loading exceptions through ES6 Promise to avoid repeated loading of resources.
It implements version control, systematic reuse, modular introduction, on-demand loading and exception capture, reducing usage complexity and performance issues, and improving usage efficiency.
Smart Images

Figure CN118656140A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a process adaptation solution based on Amap. Background Art
[0002] The loading and calling of Amap jsapi usually uses script tags to embed the resource into the HTML document header. The specific steps are as follows:
[0003] 1. Prepare the entry index.html file;
[0004] 2. In the head, use the script tag and the src provided by Amap to introduce the Amap js script;
[0005] 3. Call it in the script script at the bottom of the body.
[0006] This is also the most advanced method of introducing external scripts. This method ensures that the resources are loaded when they are used, which is very common in traditional Web projects.
[0007] In addition, Amap jsapi is divided into the following modules:
[0008] 1. AMap JS API and its plugins
[0009] 2. AMapUI and its plug-ins
[0010] 3.Loca
[0011] 4. There is a dependency relationship between them, which requires manual maintenance by developers when loading;
[0012] The above-mentioned prior art also has the following defects and shortcomings:
[0013] 1. Does not support version control;
[0014] 2. The script needs to be manually pasted and imported, which is not convenient for systematic reuse;
[0015] 3. Does not support ESmodule modularization;
[0016] 4. Preloading is not supported, causing the first screen to be blocked;
[0017] 5. Can only be loaded globally at the entrance, dependencies need to be maintained manually, and on-demand loading is not supported;
[0018] 6. Capture loading exceptions;
[0019] 7. There is a situation of repeated loading. For this reason, a process adaptive solution based on Amap is proposed. Summary of the invention
[0020] In view of this, the embodiments of the present invention hope to provide a process adaptation solution based on Amap to solve or alleviate the technical problems existing in the prior art and at least provide a beneficial option.
[0021] The technical solution of the embodiment of the present invention is implemented as follows: a solution for process adaptation based on Amap, comprising the following steps:
[0022] S1. The front-end application calls and loads Amap JSAPI;
[0023] S2. Check whether the versions of AMap, AMapUI and Loca match. If the verification fails, an exception is thrown. If the verification passes, proceed to the next step.
[0024] S3, determine whether to repeat loading, if the repeated loading is yes, then fetch the resource from the cache for loading, and end the loading process, if the repeated loading is no, proceed to the next step;
[0025] S4. Determine whether the target resource's dependency has been loaded. If the target resource's dependency has been loaded, then execute loading target (preloading), and after caching, end the loading process. If the target resource's dependency has not been loaded, then load the dependency, then execute loading of the target resource, and after caching, end the loading process.
[0026] S5. Implement the solution logic from S1 to S4 with JS code, use NodeJS and esbuild tools, package and publish it into npm package for easy use.
[0027] In some embodiments, in S2, based on the version relationship of AMap, AMapUI and Loca provided by Amap, the version of AMap is mainly used. When the AMap version is 1.x, the AMapUI version must be less than v1.1, and the Loca version must be less than v1.3;
[0028] When the AMap version is 2.x, the AMapUI version must be no less than v1.1, and the Loca version must be no less than v1.3.
[0029] In some embodiments, in S4, loading dependency is to start a new resource loading process from S2.
[0030] In some embodiments, in S4, the principle of loading the target (preloading) is to use the key necessary parameters as fixed parameters, dynamically construct a script loading tag using the js script and parameters, and insert it into the DOM to achieve the purpose of loading the Gaode js script.
[0031] In some embodiments, in S4, the loading target (preloading) uses the link tag to introduce the Amap js script, and uses the browser prefetch (link prefetching) mechanism to enable the browser to achieve the purpose of preloading by obtaining and caching the corresponding resources in advance.
[0032] In some embodiments, in S4, the loading target (preloading) loads Gaode js resources in the form of ES6 Promise, and enters the then callback of Promse after the resource loading is completed, so as to realize on-demand introduction, and capture the loading exception. When the exception is captured, enter the catch callback of Promise.
[0033] In some embodiments, in S3 and S4, after the first loading, the corresponding resource will be cached, and when the loading command is executed again later, it will first be determined whether the resource has been loaded;
[0034] If the target resource has not been loaded, the loading operation is performed;
[0035] If the target resource has been loaded, skip the resource loading process and return the target object;
[0036] Avoid duplicate resource loading.
[0037] In some embodiments, in the S5, the npm package encapsulates a package that supports ESModule modularization, supports npm introduction, compiles the target es6 standard code through the rollup build tool, and publishes it to a public or private npm platform, which can be introduced through npm to achieve modular introduction and use.
[0038] The embodiment of the present invention has the following advantages due to the adoption of the above technical solution:
[0039] 1. The present invention supports the associated version control of multiple resources through this method. When the user passes in an incorrect version, an exception will be triggered and a prompt will be given, thereby ensuring version control and avoiding a series of problems caused by version confusion.
[0040] Second, the present invention supports ESModule modularization and npm package installation, which facilitates systematic reuse and is easy to maintain and manage. The product is ES Module, which conforms to the current modularization trend.
[0041] 3. The present invention reduces the mental effort of use by providing on-demand loading, loading exception capture, automatic dependency management, preloading, and avoiding repeated loading. Compared with traditional introduction methods, there is no need to worry about exceptions and performance issues caused during the loading process, which greatly improves usage efficiency.
[0042] The above summary is for illustrative purposes only and is not intended to be limiting in any way. In addition to the illustrative aspects, embodiments and features described above, further aspects, embodiments and features of the present invention will be readily apparent by reference to the accompanying drawings and the following detailed description. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0044] Figure 1 It is a flow chart of the present invention. DETAILED DESCRIPTION
[0045] In the following, only some exemplary embodiments are briefly described. As those skilled in the art will appreciate, the described embodiments may be modified in various ways without departing from the spirit or scope of the present invention. Therefore, the drawings and descriptions are considered to be exemplary and non-restrictive in nature.
[0046] It should be noted that the terms "first", "second", "symmetrical", "array", etc. are only used to distinguish between description and position description purposes, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the definition of "first", "symmetrical", etc. can explicitly or implicitly include one or more of these features; similarly, when the quantity of certain features is not limited in the form of words such as "two" or "three", it should be noted that this feature also explicitly or implicitly includes one or more feature quantities;
[0047] In the present invention, unless otherwise clearly specified and limited, the terms such as "installation", "connection", "fixation" and the like should be understood in a broad sense; for example, it can be a fixed connection, a detachable connection, or an integral molding; it can be a mechanical connection, a direct connection, welding, or an indirect connection through an intermediate medium, or it can be the internal connection of two elements or the interaction relationship between two elements. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood based on the description and drawings combined with specific circumstances.
[0048] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0049] like Figure 1 As shown, the embodiment of the present invention provides a solution for process adaptation based on Amap, including the following steps:
[0050] S1. The front-end application calls and loads Amap JSAPI;
[0051] S2. Check whether the versions of AMap, AMapUI and Loca match. If the verification fails, an exception is thrown. If the verification passes, proceed to the next step.
[0052] S3, determine whether to repeat loading, if the repeated loading is yes, then fetch the resource from the cache for loading, and end the loading process, if the repeated loading is no, proceed to the next step;
[0053] S4. Determine whether the target resource's dependency has been loaded. If the target resource's dependency has been loaded, then execute loading target (preloading), and after caching, end the loading process. If the target resource's dependency has not been loaded, then load the dependency, then execute loading of the target resource, and after caching, end the loading process.
[0054] S5. Implement the solution logic from S1 to S4 with JS code, use NodeJS and esbuild tools, package and publish it into npm package for easy use.
[0055] In this embodiment, specifically, in S2, based on the version relationship of AMap, AMapUI and Loca provided by Amap, the AMap version will be the main one. When the AMap version is 1.x, the AMapUI version must be less than v1.1, and the Loca version must be less than v1.3;
[0056] When the AMap version is 2.x, the AMapUI version must be no less than v1.1, and the Loca version must be no less than v1.3.
[0057] In this embodiment, specifically, in S4, the loading dependency is to start a new resource loading process from S2.
[0058] In this embodiment, specifically, in S4, the principle of loading the target (preloading) is to use the key necessary parameters as fixed parameters, dynamically construct the script loading tag using the js script and parameters, and insert it into the DOM to achieve the purpose of loading the Gaode js script.
[0059] In this embodiment, specifically, in S4, the loading target (preloading) uses the link tag to introduce the Amap js script, and uses the browser prefetch (link prefetching) mechanism to enable the browser to obtain and cache the corresponding resources in advance to achieve the purpose of preloading. The above setting "link prefetching" means obtaining the external resources mentioned in the page in advance during the webpage loading process, such as pictures, videos, scripts, etc.;
[0060] The purpose of this is to improve page loading speed and user experience, because these resources will eventually need to be loaded and displayed, and pre-fetching them can reduce the delay when the page interacts;
[0061] In HTML5, you can use <link> The asynchronous attribute of the tag or <script>标签的defer属性来实现资源的预加载。
[0062] 本实施例中,具体的,在S4中,加载目标(预加载)以ES6 Promise的形式进行高德js资源加载,并在资源加载完成后进入Promse的then回调中,以此实现按需引入,并对加载异常进行捕获,捕获出现异常时,进入Promise的catch回调中,通过以上设置ES6 Promise提供了一种更加优雅的处理异步操作的方法。它代表了一个最终可能完成也可能失败的操作的结果,Promise有三种状态:
[0063] 待定(Pending):初始状态,既没有被兑现,也没有被拒绝;
[0064] 已兑现(Fulfilled):操作成功完成;
[0065] 已拒绝(Rejected):操作失败;
[0066] Promise对象本质上是一个含有then和catch方法的对象。then方法用于设置当Promise对象成功时应该执行的回调函数,catch方法用于设置当Promise对象失败时应该执行的回调函数。
[0067] 本实施例中,具体的,在S3和S4中,在首次加载后,会缓存对应的资源,后续再次执行加载命令时,会先判断此资源是否加载过;
[0068] 目标资源未加载过,则执行加载操作;
[0069] 目标资源已加载过,则跳过资源加载过程,返回目标对象;
[0070] 避免资源重复加载。
[0071] 本实施例中,具体的,在S5中,npm包封装支持ESModule模块化的包,支持npm引入,通过rollup构建工具编译目标位es6标准代码,发布至公有或私有npm平台,即可通过npm引入,实现模块化引入和使用。
[0072] 本实施例中,具体的,ESModule,即ECMAScript模块,是一种基于ECMAScript标准的模块化编程语法,用于在JavaScript中封装和组织代码,ESModule旨在解决JavaScript中全局作用域污染、命名冲突以及代码可维护性等问题;
[0073] ESModule的主要特点包括:
[0074] 封装性:每个模块有自己的作用域,模块内部定义的变量和函数不会污染全局作用域。
[0075] 命名空间:模块可以通过export语句定义公共接口,其他模块可以通过import语句导入这些接口。这样可以避免不同模块间的命名冲突。
[0076] 可维护性:模块化的代码更易于管理和维护,因为它们可以独立更新,而不会影响到其他模块。
[0077] 异步加载:ESModule支持异步加载,这意味着模块可以在需要时再加载,这样可以减少初始加载时间,提高页面性能。
[0078] 本实施例中,具体的,ES6,也称为ECMAScript 2015,是JavaScript语言标准的第六版。这一版本引入了许多重要的语法和新特性,使得JavaScript编程更加简洁、高效,以下是是一些ES6标准中引入的常用新特性:
[0079] 1、let和const:let用于声明变量,具有块级作用域;const用于声明常量,也是块级作用域,且一旦赋值后不能被重新赋值;
[0080] let x=1;
[0081] const y=2;
[0082] 2、箭头函数:一种简洁的函数写法,尤其适合与回调函数和对象的方法结合使用;
[0083] const square=x=>x*x;
[0084] 3、模板字符串:一种增强的字符串处理方式,允许你方便地创建包含变量和表达式的字符串;
[0085] const name='Alice';
[0086] const greeting=`Hello,${name}!`;
[0087] 4、解构赋值:从数组或对象中提取值并赋给变量,同时支持默认值和嵌套结构;
[0088] const[first,second,third]=['Alice','Bob','Charlie'];
[0089] const{x:a,y:b}={x:1,y:2};
[0090] 5、扩展运算符:用于数组和对象的展开,可以用来复制数组、合并数组或提取对象属性;
[0091] const arr1=[1,2];
[0092] const arr2=[...arr1,3,4];
[0093] const obj1={a:1};
[0094] const obj2={...obj1,b:2};
[0095] 6、Promise:ES6正式将Promise对象纳入标准,用于处理异步操作;
[0096]
[0097] 7、类和继承:ES6引入了类(class)的概念,使得JavaScript的面向对象编程更加直观;
[0098]
[0099]
[0100] 8、模块化:ES6引入了模块化编程,使用export和import语句来封装和导出功能
[0101]
[0102] 本实施例中,具体的,基于高德JS资源,有以下依赖关系:
[0103] AMap Plugin依赖于AMap;
[0104] AMapUI Plugin依赖于AMapUI依赖于AMap;
[0105] Loca依赖于AMap;
[0106] 此方案可直接加载以上任意模块或插件,当发现加载目标有依赖时,先判断其依赖是否已加载完成,若其依赖未加载完成,继续寻找是否包含上层依赖,否则执行加载,逐层进行,以此方案来实现自动加载依赖的目的,使用者只需关心需要什么,而不需要关注其上层依赖。
[0107] 本实施例中,具体的,对于本领域技术人员而言,还可以进一步意识到结合本文中所公开的实施例描述的各示例的单元及算法步骤,能够以电子硬件、计算机软件或者二者的结合来实现,为了清楚地说明硬件和软件的可互换性,在上述说明中已经按照功能一般性地描述了各示例的组成及步骤。这些功能究竟以硬件还是软件方式来执行,取决于技术方案的特定应用和设计约束条件。专业技术人员可以对每个特定的应用来使用不同方法来实现所描述的功能,但是这种实现不应认为超出本发明的范围。
[0108] 同时本领域技术人员可以理解实现上述所有实施例方法中的全部或部分流程,是可以通过计算机程序来指令相关的硬件来完成,所述的计算机程序可存储于一非易失性计算机可读取存储介质中,该计算机程序在执行时,可包括如上述各方法的实施例的流程。其中,本申请所提供的和实施例中所使用的对存储器、存储、数据库或其它介质的任何引用,均可包括非易失性和 / 或易失性存储器。非易失性存储器可以包括只读存储器(ROM)、可编程ROM(PROM)、电可编程ROM(EPROM)、电可擦除可编程ROM(EEPROM)或闪存。易失性存储器可包括随机存取存储器(RAM)或者外部高速缓冲存储器。作为说明而非局限,RAM以多种形式可得,诸如静态RAM(SRAM)、动态RAM(DRAM)、同步DRAM(SDRAM)、双速据率SDRAM(SSRSDRAM)、增强型SDRAM(ESDRAM)、同步链路(Synchlink)DRAM(SLDRAM)、存储器总线(Rambus)直接RAM(RDRAM)、直接存储器总线动态RAM(DRDRAM)、以及存储器总线动态RAM(RDRAM)等。
[0109] 本实施例中,具体的,可以全部或部分地通过软件、硬件、固件或其他任意组合来实现,当使用软件实现时,上述实施例可以全部或部分地以计算机程序货物的形式实现;
[0110] 在计算机上加载或执行所述计算机指令或计算机程序时,全部或部分地产生按照本申请实施例所述的流程或功能。所述计算机可以为通用计算机、专用计算机、计算机网络、或者其他可编程装置。所述计算机指令可以存储在计算机可读存储介质中,或者从一个计算机可读存储介质向另一个计算机可读存储介质传输,例如,所述计算机指令可以从一个网站站点、计算机、服务器或数据中心通过有线或无线(例如红外、无线、微波等)方式向另一个网站站点、计算机、服务器或数据中心进行传输。所述计算机可读存储介质可以是计算机能够存取的任何可用介质或者是包含一个或多个可用介质集合的服务器、数据中心等数据存储设备。所述可用介质可以是磁性介质(例如,软盘、硬盘、磁带)、光介质(例如,DVD)、或者半导体介质。半导体介质可以是固态硬盘。
[0111] 应理解,在本申请的各种实施例中,上述各过程的序号的大小并不意味着执行顺序的先后,各过程的执行顺序应以其功能和内在逻辑确定,而不应对本申请实施例的实施过程构成任何限定。
[0112] 本领域普通技术人员可以意识到,结合本文中所公开的实施例描述的各示例的单元及算法步骤,能够以电子硬件、或者计算机软件和电子硬件的结合来实现。这些功能究竟以硬件还是软件方式来执行,取决于技术方案的特定应用和设计约束条件。专业技术人员可以对每个特定的应用来使用不同方法来实现所描述的功能,但是这种实现不应认为超出本申请的范围。
[0113] 所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,上述描述的系统的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。
[0114] 所述功能如果以软件功能单元的形式实现并作为独立的货物销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本申请的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的部分可以以软件货物的形式体现出来,该计算机软件货物存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本申请各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(read-only memory,ROM)、随机存取存储器(random access memory,RAM)、磁碟或者光盘等各种可以存储程序代码的介质。
[0115] 以上所述,仅为本发明的具体实施方式,但本发明的保护范围并不局限于此,任何熟悉本技术领域的技术人员在本发明揭露的技术范围内,可轻易想到其各种变化或替换,这些都应涵盖在本发明的保护范围之内。因此,本发明的保护范围应以所述权利要求的保护范围为准。< / script>
Claims
1. A process adaptive solution based on Amap, characterized in that: The following steps are involved: S1. The front-end application calls and loads Amap JSAPI; S2. Check whether the versions of AMap, AMapUI and Loca match. If the verification fails, an exception is thrown. If the verification passes, proceed to the next step. S3, determine whether to repeat loading, if the repeated loading is yes, then fetch the resource from the cache for loading, and end the loading process, if the repeated loading is no, proceed to the next step; S4. Determine whether the target resource's dependency has been loaded. If the target resource's dependency has been loaded, then execute loading target (preloading), and after caching, end the loading process. If the target resource's dependency has not been loaded, then load the dependency, then execute loading of the target resource, and after caching, end the loading process. S5. Implement the solution logic from S1 to S4 with JS code, use NodeJS and esbuild tools, package and publish it into npm package for easy use.
2. The process adaptive solution based on Amap according to claim 1 is characterized by: In S2, based on the version relationship of AMap, AMapUI and Loca provided by Amap, the AMap version will be the main one. When the AMap version is 1.x, the AMapUI version must be less than v1.1, and the Loca version must be less than v1.3; When the AMap version is 2.x, the AMapUI version must be no less than v1.1, and the Loca version must be no less than v1.
3.
3. The process adaptive solution based on Amap according to claim 1 is characterized by: In S4, loading dependency is to start a new resource loading process from S2.
4. The process adaptive solution based on Amap according to claim 1 is characterized by: In S4, the principle of loading the target (preloading) is to use the key necessary parameters as fixed parameters, dynamically construct a script loading tag using the js script and parameters, and insert it into the DOM to achieve the purpose of loading the Amap js script.
5. The process adaptive solution based on Amap according to claim 1 is characterized by: In the S4, the loading target (preloading) uses the link tag to introduce the Amap js script, and uses the browser prefetch (link prefetching) mechanism to enable the browser to achieve the purpose of preloading by obtaining and caching the corresponding resources in advance.
6. The process adaptive solution based on Amap according to claim 1 is characterized by: In the S4, the loading target (preloading) loads the Gaode js resource in the form of ES6 Promise, and enters the then callback of Promse after the resource loading is completed, so as to realize on-demand introduction and capture the loading exception. When the exception is captured, it enters the catch callback of Promise.
7. The process adaptive solution based on Amap according to claim 1 is characterized by: In S3 and S4, after the first loading, the corresponding resource will be cached, and when the loading command is executed again later, it will first be determined whether the resource has been loaded; If the target resource has not been loaded, the loading operation is performed; If the target resource has been loaded, skip the resource loading process and return the target object; Avoid duplicate resource loading.
8. The process adaptive solution based on Amap according to claim 1 is characterized by: In the S5, the npm package encapsulates the package that supports ESModule modularization, supports npm introduction, compiles the target es6 standard code through the rol lup build tool, and publishes it to the public or private npm platform, which can be introduced through npm to achieve modular introduction and use.
