Get X And Y Pixel Of Touched MapTile In Carto Mobile SDK

- 1 answer

I have a RasterTileLayer for showing wms layer and i need getting features of touched area from geoServer; but geoServer needs x and y coordinate of touched mapTile in range of 0 to 256 (cause tile size set to 256); but i don`t know how to get it or calculate it,Do you have any solution?



in general you will receive click events by registering your RasterTileEventListener. But the argument you receive (RasterTileClickInfo) does not currently provide you exact click coordinates. In SDK versions prior to 4.1.4 you have to do some calculations manually. The following snippet should help you:

            rasterLayer.setRasterTileEventListener(new RasterTileEventListener() {
            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;

Note that you may need to flip the y-coordinate as it starts from the bottom.

As a side note, SDK 4.1.4 exposes TileUtils class with some static methods that perform the same calculations used above.