在Carto移动SDK中获取触摸的MapTile的x和y像素

问题描述 投票:0回答:1

我有一个用于显示wms图层的RasterTileLayer,我需要从geoServer获取触摸区域的功能;但是geoServer需要触摸的mapTile的x和y坐标,范围为0到256(因为tile大小设置为256);但我不知道如何得到或计算它,你有任何解决方案吗?

android maps cartodb nutiteq carto-mobile
1个回答
0
投票

通常,您将通过注册RasterTileEventListener来接收单击事件。但是您收到的参数(RasterTileClickInfo)目前不能为您提供准确的点击坐标。在4.1.4之前的SDK版本中,您必须手动进行一些计算。以下代码段可以帮助您:

            rasterLayer.setRasterTileEventListener(new RasterTileEventListener() {
            @Override
            public boolean onRasterTileClicked(RasterTileClickInfo clickInfo) {
                MapTile mapTile = clickInfo.getMapTile();
                Projection proj = rasterLayer.getDataSource().getProjection();
                double projTileWidth = proj.getBounds().getDelta().getX() / (1 << mapTile.getZoom());
                double projTileHeight = proj.getBounds().getDelta().getY() / (1 << mapTile.getZoom());
                double projTileX0 = proj.getBounds().getMin().getX() + mapTile.getX() * projTileWidth;
                double projTileY0 = proj.getBounds().getMin().getY() + ((1 << mapTile.getZoom()) - 1 - mapTile.getY()) * projTileHeight;
                double normTileX = (clickInfo.getClickPos().getX() - projTileX0) / projTileWidth;
                double normTileY = (clickInfo.getClickPos().getY() - projTileY0) / projTileHeight;
                Log.d("", "Clicked at: " + (int) (normTileX * 256) + ", " + (int) (normTileY * 256));
                return true;
            }
        });

请注意,您可能需要在从底部开始时翻转y坐标。

作为旁注,SDK 4.1.4使用一些静态方法公开TileUtils类,这些方法执行上面使用的相同计算。

© www.soinside.com 2019 - 2024. All rights reserved.