2

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,

Community
  • 1
  • 1
HelloWorld
  • 2,275
  • 18
  • 29

2 Answers2

2

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.

steve hannah
  • 4,586
  • 11
  • 18
1

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.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • do you mean the `MapComponent` from `package com.codename1.maps` or the `MapContainer`. The former does not declare `Projection` or `BoundingBox` object and I cannot find the `MapContainer` sources. Would you mind guiding me further? – HelloWorld Feb 09 '17 at 18:12
  • `MapContainer` is a part of the native maps cn1lib which is [here](https://github.com/codenameone/codenameone-google-maps). Since it's implemented as a cn1lib you can also easily edit the native code which is sometimes less intuitive in Codename One itself. It's also not as risky. – Shai Almog Feb 10 '17 at 04:42
  • Thanks @Shai. Meanwhile for the same goal I tried something with `getCoordAtPosition` applied to the MapContainer but I don't understand the results. The map center is at (**Lat : 48.8574 ; Lon : 2.0654**), and I tried `// Get the map borders` `Coord NE = currentMap.getCoordAtPosition(currentMap.getWidth(), 0); Coord SW = currentMap.getCoordAtPosition(0, currentMap.getHeight());` In the simulator I get **Lat SW = 48.8584 lat NE = 48.8612** _(NOK because both lat > map center lat)_ **Lon SW = 2.0625 lon NE = 2.0679** _(OK)_. Why is the SW latitude wrong ? – HelloWorld Feb 10 '17 at 21:33
  • I'm not a map expert but I recall there are several "projection" types or something like that so maybe the map is using one of those? – Shai Almog Feb 11 '17 at 05:53
  • So the method `getCoordAtPosition` does not take the projection into account. I understood from the method name that I would get the lon;lat coord of any xy point on the map. Is my understanding correct? – HelloWorld Feb 11 '17 at 06:21
  • It should but it might use a different type of projection than the one used by MapComponent, sorry if I'm vague here... It's not intentional, just ignorance about this specific niche – Shai Almog Feb 12 '17 at 04:47
  • I get what you meant now! Thanks for the details! If you're right (the projection in MapContainer is different than in MapComponent) then can it be considered as a bug of `getCoordAtPosition`? – HelloWorld Feb 12 '17 at 06:26
  • That's possible, I'm not sure it's something we want to fix though. Personally, I'd align with native maps rather than with MapComponent. We are moving towards using HTML maps in the `MapContainer` instead of MapComponent as the fallback. They look and function better on the devices and now that we have z-ordering in the peers they are a better option – Shai Almog Feb 13 '17 at 04:50
  • Do you mean this behaviour only occurs in the simulator (which falls back to MapComponent) and will not occur on the real iOS / Android devices which do use MapContainer ? If so then the problem is solved! – HelloWorld Feb 13 '17 at 06:02
  • I don't know, but the coordinates might be valid and you might need to run them thru a projection – Shai Almog Feb 14 '17 at 05:19