How to get my MapContainer bounding box in Codenam

2020-07-27 02:53发布

问题:

My Codename One app features a MapContainer. I need to show points of interest (POIs) on it which coordinates reside on the server. There can be hundreds (maybe thousands in the future) of such POIs on the server. That's why I would like to only download from the server the POIs that can be shown on the map. Consequently I need to get the map boundaries to pass them to the server.

I read this for Android and this other SO question for iOS and the key seems to get the map Projection and the map bounding box. However the getProjection() method or the getBoundingBox() seem not to be exposed.

A solution could be to mix the coordinates from getCameraLocation() which is the map center and getZoom() to infer those boundaries. But it may vary depending on the device (see the shown area can be larger).

How can get the map boundaries in Codename one ?

Any help appreciated,

Cheers,

回答1:

The problem is in the javadocs for getCoordAtPosition(). This will be corrected. getCoordAtPosition() expects absolute coordinates, not relative.

E.g

Coord NE = currentMap.getCoordAtPosition(currentMap.getWidth(), 0); 
Coord SW = currentMap.getCoordAtPosition(0, currentMap.getHeight());

Should be

Coord NE = currentMap.getCoordAtPosition(currentMap.getAbsoluteX() + currentMap.getWidth(), currentMap.getAbsoluteY()); 
Coord SW = currentMap.getCoordAtPosition(currentMap.getAbsoluteX(), currentMap.getAbsoluteY() + currentMap.getHeight());

I tried this out on the coordinates that you provided and it returns valid results.

EDIT March 21, 2017 : It turns out that some of the platforms expected relative coordinates, and others expected absolute coordinates. I have had to standardize it, and I have chosen to use relative coordinates across all platforms to be consistent with the Javadocs. So your first attempt:

Coord NE = currentMap.getCoordAtPosition(currentMap.getWidth(), 0); 
Coord SW = currentMap.getCoordAtPosition(0, currentMap.getHeight());

Will now work in the latest version of the library.

I have also added another method : getBoundingBox() that will get the bounding box for you without worrying about relative/absolute coordinates.



回答2:

This is probably something that can be exposed easily by forking the project and providing a pull request. We're currently working on updating the map component so this is a good time to make changes and add features.



标签: codenameone