Get X And Y Pixel Of Touched MapTile In Carto Mobile SDK
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?
Answer
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() {
@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;
}
});
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.
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View