简体   繁体   English

openlayers web 应用程序在移动设备上运行速度非常慢(Android Chrome)

[英]openlayers web application runs very slow from mobile (Android Chrome)

I know this might sound like a typical kind of question that should be closed, but I hit a wall and don't know where to look for a solution, so hopefully, I'll provide enough details to make this question acceptable for SO.我知道这听起来像是一个应该关闭的典型问题,但是我碰壁了,不知道在哪里寻找解决方案,所以希望我能提供足够的细节来使这个问题可以被 SO 接受。

I am developing an OpenLayers web application that can be accessed here .我正在开发一个可以在此处访问的 OpenLayers web 应用程序。

Its source code is in this github repo .它的源代码在这个 github repo中。

If you try the app from a computer, the user experience should be fine (at least, this is my personal experience).如果您从计算机上试用该应用程序,用户体验应该很好(至少,这是我的个人体验)。

However, when I try it from my mobile browser (Chrome, my phone is a Google Pixel 2), it is extremely slow.但是,当我从我的移动浏览器(Chrome,我的手机是 Google Pixel 2)尝试它时,它非常慢。

In particular, every time I try to interact with the app by touching the screen to (eg) navigate the map, there is a considerable lag between the time I touch the screen and the response of the application.特别是,每次我尝试通过触摸屏幕与应用程序交互(例如)导航 map 时,在我触摸屏幕的时间和应用程序的响应之间存在相当大的延迟。

I even tried to use ChromeDevTools console to inspect the network console having my phone plugged in with a USB cable, but could not understand what could be the cause of this slowness.我什至尝试使用 ChromeDevTools 控制台检查使用 USB 电缆插入手机的网络控制台,但无法理解导致速度缓慢的原因。

Some details:一些细节:

  1. I am using OpenLayers version 6.2.1我正在使用 OpenLayers 版本 6.2.1
  2. I am using TypeScript to develop this app我正在使用 TypeScript 来开发这个应用程序
  3. I am using parcel bundler to bundle it in a dist folder我正在使用包裹捆绑器将其捆绑在 dist 文件夹中
  4. I copy my built files to my server to serve the app我将构建的文件复制到我的服务器以提供应用程序

The only thing I am noticing is that the built files have weird names, like src.9f4704d2.css .我唯一注意到的是构建文件的名称很奇怪,例如src.9f4704d2.css

I guess this is caused by the bundler (parcel), when I run the command npm run build .我猜这是由捆绑程序(包裹)引起的,当我运行命令npm run build时。

But I sincerely have no idea if this might be an issue.但我真的不知道这是否是一个问题。

However, if someone could help me shedding a light on this, I'd be very grateful.但是,如果有人可以帮助我阐明这一点,我将不胜感激。 Also, I can provide much more detail if requested, but my main problem is that i don't know exactly where to look for solving the problem.此外,如果需要,我可以提供更多详细信息,但我的主要问题是我不确切知道在哪里寻找解决问题的方法。


EDIT 1编辑 1

I am using @import rules in my CSS, and I read here that我在我的 CSS 中使用@import规则,我在这里读到

@import rules can be nested so the browser must load and parse each file in series. @import 规则可以嵌套,因此浏览器必须依次加载和解析每个文件。

I don't know if this is the problem, but I will try using multiple <link> tags instead and see if performance gets better...我不知道这是否是问题所在,但我会尝试使用多个<link>标签,看看性能是否变得更好......


EDIT 2编辑 2

The @import replacement with multiple <link> tags did not solve the problem.用多个<link>标签替换@import并没有解决问题。


EDIT 3 (PROBABLY FOUND THE ISSUE)编辑 3(可能发现问题)

Ok, I narrowed down the problem to be on the layers I am creating.好的,我将问题缩小到我正在创建的图层上。

I am actually parsing three WMS urls, extracting each layer out of them, and adding a new ImageLayer with a ImageWMS source for each of them to the map.我实际上是在解析三个 WMS url,从中提取每一层,并将一个带有ImageWMS源的新ImageLayer添加到 map。

The first thing I did that speeded up the application was to use TileImage and TileWMS instead of ImageLayer and ImageWMS .我为加快应用程序所做的第一件事是使用TileImageTileWMS而不是ImageLayerImageWMS

The relavant code is below:相关代码如下:

import TileLayer from 'ol/layer/Tile';
import TileWMS from 'ol/source/TileWMS';

export class OperationalLayer{
    private operationalLayer: TileLayer;
    id: String;
    
    constructor(url: string, name: string, id: string) {
        const source = new TileWMS({
            params: {'LAYERS': name},
            url: url,
        });

        this.id = id;

        this.operationalLayer = new TileLayer({
            source: source,
        })
    }

    getLayer() {
        return this.operationalLayer;
    }
}

Still, the application was very laggish.尽管如此,该应用程序仍然非常滞后。

Then I tried using only one url thus adding only 3 layers in total to my map.然后我尝试只使用一个 url,因此我的 map 总共只添加了 3 层。

Now it runs acceptably.现在它运行可以接受。

I think that the main point is that I need to focus on the best strategy to add my layers to the map: whether to have one ImageWMS with all WMS layers at once, or one ImageWMS per layer.我认为主要的一点是我需要专注于将我的层添加到 map 的最佳策略:是同时拥有一个带有所有 WMS 层的 ImageWMS,还是每层一个 ImageWMS。

The latter (which is what I am doing), would give me the possibility to turn on/off each layer easily;后者(这就是我正在做的),可以让我轻松打开/关闭每一层; the former will be faster I guess.我猜前者会更快。

Or is there an alternative i didn't consider?或者有没有我没有考虑过的替代方案?

You should use viewport meta on your page.您应该在页面上使用视口元数据。
Something like:就像是:

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

This will will fix the page size and prevent from resizing the whole page.这将修复页面大小并防止调整整个页面的大小。 This will also reduce the page size and the pixels handled in the viewport.这也将减少页面大小和视口中处理的像素。

You can also look at meta for mobile webapp to enhance your app, such as web-app-capable tag.您还可以查看移动 webapp 的 meta以增强您的应用程序,例如web-app-capable标记。

Try to use fixed position for your map and your menu bar (and all page that may cover the map) to enhance rendering.尝试为您的 map 和菜单栏(以及可能覆盖地图的所有页面)使用固定的 position 以增强渲染。

Use TileWMS: the tiles will be cached and not requested on each move (only the new tiles will be requested).使用 TileWMS:切片将被缓存,并且不会在每次移动时请求(只会请求新切片)。 There is also an hdpi options in the layers definition that will make the image more fine but enlarge canvas size (and drawing operations), try to set it to false and look at result to make your choice.图层定义中还有一个hdpi选项,可以使图像更精细但放大 canvas 大小(和绘图操作),尝试将其设置为 false 并查看结果以做出选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM