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

时间:2018-09-09 09:10:32

标签: android maps cartodb nutiteq carto-mobile

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

1 个答案:

答案 0 :(得分: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类,这些静态方法执行与上面使用的相同的计算。