Qt LightMaps Map Marker

For the past weeks I was working on new app which includes GPS positioning and maps. My first idea was to use Google Maps, but unfortunately terms and conditions are not acceptable so I had to drop the idea.

After some serious Google’ing I decided to use Open Street Map solution. The code example of implementation could be found here. The main problem I faced with example it didn’t include the map marker, which was a must for my application so I had to find some solution how to stick the marker on open street maps.

After long hours of mapping the logic behind I found that latitude and longitude coordinates changing when the map is panned. This came as a surprise as I was 100% sure that those things won’t change. After such discovery, the rest was easy…

To add a marker you need to:
1. Store your GPS data separately. To do that I added another method in LightMaps class – void setFixedLatLng(qreal lat, qreal lng) which sets the fLatitude and fLongitude variables in SlippyMap class.
2. On top of SlippyMap::render method I added these lines:

int mx = 0, my = 0; // x and y for marker
qreal latdiff, lngdiff; // storing reminder
QPointF center = tileForCoordinate(fLatitude, fLongitude, zoom); // getting tile points for real gps point
int cx = qFloor(center.x());
int cy = qFloor(center.y());

3. After QRect box = tileRect(tp) I added

if (tp.x() == cx && tp.y() == cy) {
mx = box.x();
my = box.y();
latdiff = qAbs(tp.x() - center.x());
lngdiff = qAbs(tp.y() - center.y());

4. And the last adjustment was at the very end of the method where I added this:

// marker
if (mx != 0 && my != 0) {
QIcon marker = QIcon(":/img/marker.png");
p->drawPixmap((mx+(latdiff*256.0)-16), (my+(lngdiff*256.0)-25), marker.pixmap(16,25));

Note that 16×25 is my marker size.

It might be not the best solution, but at least it works well. Last problem in my app is solved!

Posted in Other