summaryrefslogtreecommitdiff
path: root/src/navigation/map-viewer/mapviewercontrol-server-plugin/genivi_mapviewer_mapviewercontrol.cxx
blob: dd03d80e40591e89c8799cd1c7a9c4e9fdf50caf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
/**
* @licence app begin@
* SPDX-License-Identifier: MPL-2.0
*
* \copyright Copyright (C) 2013-2014, PCA Peugeot Citroen
*
* \file genivi_mapviewer_mapviewercontrol.cxx
*
* \brief This file is part of the Navit POC.
*
* \author Martin Schaller <martin.schaller@it-schaller.de>
* \author Philippe Colliot <philippe.colliot@mpsa.com>
* \author Tanibata, Nobuhiko <NOBUHIKO_TANIBATA@denso.co.jp>
*
* \version 1.0
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see http://www.genivi.org/.
*
* @licence end@
*/
#include <dbus-c++/glib-integration.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
#if LM
#include <ilm/ilm_client.h>
#include <ilm/ilm_control.h>
//Align with Qt Surfaces = 8000 + Layer.
#ifndef FSA_SURFACE
#define SURFACE_OFFSET 8000
#define FSA_SURFACE SURFACE_OFFSET + getpid()
#endif
#endif

#include <config.h>
#define USE_PLUGINS 1
#include <navit/debug.h>
#include <navit/plugin.h>
#include <navit/item.h>
#include <navit/config_.h>
#include <navit/navit.h>
#include <navit/event.h>
#include <navit/point.h>
#include <navit/graphics.h>
#include <navit/coord.h>
#include <navit/transform.h>
#include <navit/map.h>
#include <navit/mapset.h>
#include <navit/callback.h>
#include <navit/vehicle.h>
#include <navit/attr.h>
#include <navit/xmlconfig.h>
#include <navit/layout.h>

#include <CommonAPI/CommonAPI.hpp>
#include <CommonTypes.hpp>
#include <NavigationTypes.hpp>
#include <MapViewerControlStubDefault.hpp>
#include <MapMatchedPositionProxy.hpp>
#include <RoutingProxy.hpp>
#include <navigationcore/SessionProxy.hpp>
#include "log.h"

DLT_DECLARE_CONTEXT(gCtx);

#if (!DEBUG_ENABLED)
#undef dbg
#define dbg(level,...) ;
#endif

#define ZOOM_ZERO_METER_WIDTH 33250 //width in millimeter of the real area for the minimum zoom
#define ZOOM_ZERO_METER_HEIGHT 21970 //height in millimeter of the real area for the minimum zoom

using namespace v4::org::genivi::navigation::mapviewer;
using namespace v4::org::genivi::navigation::navigationcore;
using namespace v4::org::genivi::navigation;
using namespace v4::org::genivi;

class MapViewerControlServerStub;
class MapMatchedPositionClientProxy;
class RoutingClientProxy;
class NavigationCoreSessionClientProxy;

static std::shared_ptr < CommonAPI::Runtime > runtime;
static NavigationTypes::Handle m_navigationcore_session;
static MapMatchedPositionClientProxy *mp_mapMatchedPositionClientProxy;
static RoutingClientProxy* mp_routingClientProxy;
static NavigationCoreSessionClientProxy* mp_navigationCoreSessionClientProxy;

class DisplayedRoute
{
	char *m_filename;
	std::vector <struct coord> m_coordinates;
	struct mapset *m_mapset;
	bool m_shown;

	void AddGeoCoordinateD(double lat, double lon);
    bool AddSegment(Routing::RouteSegment map);
	void WriteSegment(FILE *out);
	public:
	uint32_t m_handle;
	struct attr m_map;
    NavigationTypes::Handle RouteHandle();
	void Show();
	void Hide();
    DisplayedRoute(class MapViewerControlObj *mapviewer, uint8_t RouteSession, NavigationTypes::Handle RouteHandle, struct mapset *mapset);
	~DisplayedRoute();
};

class MapViewerControlObj
{
	public:
	struct attr m_navit,m_vehicle,m_graphics;
	struct mapset *m_mapset;
	uint32_t m_handle;
	double m_scrolldirection, m_scrollspeed;
    double m_rotationangle, m_rotationanglepersecond;

	struct callback *m_postdraw_callback;
	struct callback *m_move_callback;
	bool m_force_draw;
    MapViewerControl::MapPerspective m_perspective;
	bool m_follow_car;
    MapViewerControlServerStub *mp_mapviewercontrol;
	std::vector<DisplayedRoute *> m_displayed_routes;
    struct point m_pan;
	int m_pan_action;
    uint16_t m_width;
    uint16_t m_height;

    MapViewerControlObj(MapViewerControlServerStub *mapviewercontrol, NavigationTypes::Handle handle, const MapViewerControl::Dimension &MapViewSize);
    ~MapViewerControlObj();
    void MoveMap(void);
    void SetFollowCarMode(NavigationTypes::Handle SessionHandle, bool active);
    void GetFollowCarMode(bool& active);
    void SetCameraHeadingAngle(NavigationTypes::Handle SessionHandle, double angle);
    void GetCameraHeadingAngle(double &angle);
    void SetCameraTiltAngle(NavigationTypes::Handle SessionHandle, double angle);
    void GetCameraTiltAngle(int32_t &angle);
    void SetCameraRollAngle(NavigationTypes::Handle SessionHandle, double angle);
    void GetCameraRollAngle(int32_t &angle);
    void SetCameraDistanceFromTargetPoint(NavigationTypes::Handle SessionHandle, double distance);
    void GetCameraDistanceFromTargetPoint(uint32_t &distance);
    void SetCameraHeight(NavigationTypes::Handle SessionHandle, double height);
    void GetCameraHeight(uint32_t &height);
    void SetCameraHeadingTrackUp(NavigationTypes::Handle SessionHandle);
    void SetMapViewPerspective(NavigationTypes::Handle SessionHandle, MapViewerControl::MapPerspective MapViewPerspectiveMode);
    void GetMapViewPerspective(MapViewerControl::MapPerspective &MapViewPerspectiveMode);
    void GetScaleList(std::vector< MapViewerControl::MapScale >& ScalesList);
    void SetMapViewScale(NavigationTypes::Handle SessionHandle, uint8_t ScaleID);
    void SetMapViewScaleByDelta(NavigationTypes::Handle SessionHandle, int16_t ScaleDelta);
    void GetMapViewScale(uint8_t& ScaleID, MapViewerControl::MapScaleType &IsMinMax);
    void GetMapViewType(MapViewerControl::MapViewType &MapViewType);
    void SetMapViewTheme(NavigationTypes::Handle SessionHandle, MapViewerControl::MapTheme mapViewTheme);
    void GetMapViewTheme(MapViewerControl::MapTheme &mapViewTheme);
    void SetTargetPoint(NavigationTypes::Handle SessionHandle, NavigationTypes::Coordinate3D target);
    void GetTargetPoint(NavigationTypes::Coordinate3D &target);
    void SetMapViewPan(NavigationTypes::Handle SessionHandle, MapViewerControl::PanAction panningAction, MapViewerControl::Pixel p);
    void GetMapViewPan(const int32_t &panningAction, MapViewerControl::Pixel &p);
    void SetMapViewRotation(NavigationTypes::Handle SessionHandle, double rotationAngle, double rotationAnglePerSecond);
    void GetMapViewRotation(int32_t& rotationAngle, int32_t& _rotationAnglePerSecond);
    void SetMapViewBoundingBox(NavigationTypes::Handle SessionHandle, const NavigationTypes::Rectangle& boundingBox);
    void GetMapViewBoundingBox(NavigationTypes::Rectangle& boundingBox);
    void GetDisplayedRoutes(std::vector<MapViewerControl::DisplayedRoute> &displayedRoutes);
    void DisplayRoute(NavigationTypes::Handle SessionHandle, NavigationTypes::Handle RouteHandle, bool highlighted);
    void HideRoute(NavigationTypes::Handle SessionHandle, NavigationTypes::Handle RouteHandle);
    void ConvertPixelCoordsToGeoCoords(NavigationTypes::Handle SessionHandle, const std::vector<MapViewerControl::Pixel> &pixelCoordinates, std::vector<NavigationTypes::Coordinate2D> &GeoCoordinates);
    void ConvertGeoCoordsToPixelCoords(NavigationTypes::Handle SessionHandle, const std::vector<NavigationTypes::Coordinate2D> &geoCoordinates, std::vector<MapViewerControl::Pixel> &pixelCoordinates);
};

static std::map<uint32_t, MapViewerControlObj *> mp_handles;

static void positionVehicleNavitUpdate(std::shared_ptr<MapMatchedPositionProxyDefault> pos, struct vehicle *v);

class  MapMatchedPositionClientProxy
{
    public:

    struct callback *mp_callback;
    MapViewerControlObj *m_mapviewerobj;
    std::shared_ptr<MapMatchedPositionProxyDefault> myServiceMapMatchedPosition;

    void connectToMapViewer(MapViewerControlObj *obj)
    {
        m_mapviewerobj=obj;
    }

    void connectToVehicle(struct vehicle *v)
    {
        mp_callback=callback_new_2(callback_cast(positionVehicleNavitUpdate), myServiceMapMatchedPosition, v);
    }

    MapMatchedPositionClientProxy(const std::string domain, const std::string instance)
    {
        myServiceMapMatchedPosition = runtime->buildProxy<MapMatchedPositionProxy>(domain, instance);

        while (!myServiceMapMatchedPosition->isAvailable()) {
            usleep(10);
        }
    }

    void setListeners()
    {
        myServiceMapMatchedPosition->getPositionUpdateEvent().subscribe([&](const std::vector<MapMatchedPosition::PositionItemKey>& changedValues) {
            positionUpdate(changedValues);});
    }

    void positionUpdate(const std::vector<MapMatchedPosition::PositionItemKey>& changedValues)
    {
        event_add_timeout(0, 0, mp_callback);
    }
};

static void positionVehicleNavitUpdate(std::shared_ptr<MapMatchedPositionProxyDefault> pos, struct vehicle *v)
{
    std::vector< MapMatchedPosition::PositionItemKey > valuesToReturn;
    CommonAPI::CallStatus status;
    MapMatchedPosition::getPositionError _error;
    MapMatchedPosition::PositionItemDict _position;
    valuesToReturn.push_back(MapMatchedPosition::PositionItemKey::LATITUDE);
    valuesToReturn.push_back(MapMatchedPosition::PositionItemKey::LONGITUDE);
    valuesToReturn.push_back(MapMatchedPosition::PositionItemKey::SPEED);
    valuesToReturn.push_back(MapMatchedPosition::PositionItemKey::HEADING);

    pos->getPosition(valuesToReturn,status,_error,_position);
    if (_position.find(MapMatchedPosition::PositionItemKey::SPEED) != _position.end()) {
        struct attr position_speed={attr_position_speed};
        double speed;
        position_speed.u.numd=&speed;
        speed=_position[MapMatchedPosition::PositionItemKey::SPEED].get<double>();
        vehicle_set_attr(v, &position_speed);
    }
    if (_position.find(MapMatchedPosition::PositionItemKey::HEADING) != _position.end()) {
        struct attr position_direction={attr_position_direction};
        double direction;
        position_direction.u.numd=&direction;
        direction=_position[MapMatchedPosition::PositionItemKey::HEADING].get<double>();
        vehicle_set_attr(v, &position_direction);
    }
    if (_position.find(MapMatchedPosition::PositionItemKey::LATITUDE) != _position.end() && _position.find(MapMatchedPosition::PositionItemKey::LONGITUDE) != _position.end()) {
        struct attr position_coord_geo={attr_position_coord_geo};
        struct coord_geo g;
        position_coord_geo.u.coord_geo=&g;
        g.lat=_position[MapMatchedPosition::PositionItemKey::LATITUDE].get<double>();
        g.lng=_position[MapMatchedPosition::PositionItemKey::LONGITUDE].get<double>();
        dbg(lvl_debug,"update %f %f\n",g.lat,g.lng);
        vehicle_set_attr(v, &position_coord_geo);
    }
}

class RoutingClientProxy
{
    public:

    MapViewerControlObj *m_mapviewerobj;
    std::shared_ptr<RoutingProxyDefault> myServiceRouting;

    RoutingClientProxy(const std::string & domain, const std::string & instance)
    {
        myServiceRouting = runtime->buildProxy<RoutingProxy>(domain, instance);
        while (!myServiceRouting->isAvailable()) {
            usleep(10);
        }
    }

    void connectToMapViewer(MapViewerControlObj *obj)
    {
        m_mapviewerobj=obj;
    }

    void setListeners()
    {
        myServiceRouting->getRouteCalculationFailedEvent().subscribe([&](const NavigationTypes::Handle& routeHandle, const Routing::CalculationError& errorCode, const Routing::UnfullfilledRoutePreference& unfullfilledPreferences) {
            routeCalculationFailed(routeHandle,errorCode,unfullfilledPreferences);});
        myServiceRouting->getRouteCalculationSuccessfulEvent().subscribe([&](const NavigationTypes::Handle& routeHandle, const Routing::UnfullfilledRoutePreference& unfullfilledPreferences) {
            routeCalculationSuccessful(routeHandle,unfullfilledPreferences);});
    }

    void routeCalculationFailed(const NavigationTypes::Handle& routeHandle, const Routing::CalculationError& errorCode, const Routing::UnfullfilledRoutePreference& unfullfilledPreferences)
    {
        std::vector<DisplayedRoute *>::iterator it;

        for (it=m_mapviewerobj->m_displayed_routes.begin() ; it < m_mapviewerobj->m_displayed_routes.end(); it++ ) {
            if (*it && (*it)->m_handle == routeHandle) {
                (*it)->Hide();
            }
        }
    }

    void routeCalculationSuccessful(const NavigationTypes::Handle& RouteHandle, const Routing::UnfullfilledRoutePreference& unfullfilledPreferences)
    {
        std::vector<DisplayedRoute *>::iterator it;
        for (it=m_mapviewerobj->m_displayed_routes.begin() ; it < m_mapviewerobj->m_displayed_routes.end(); it++ ) {
            if (*it && (*it)->m_handle == RouteHandle) {
                (*it)->Hide();
                delete(*it);
                *it=new DisplayedRoute(m_mapviewerobj,1,RouteHandle,m_mapviewerobj->m_mapset);
                (*it)->Show();
            }
        }
    }

};

class  NavigationCoreSessionClientProxy
{
    public:
    std::shared_ptr<SessionProxyDefault> myServiceNavigationCoreSession;

    NavigationCoreSessionClientProxy(const std::string domain, const std::string instance)
    {
        myServiceNavigationCoreSession = runtime->buildProxy<SessionProxy>(domain, instance);

        while (!myServiceNavigationCoreSession->isAvailable()) {
            usleep(10);
        }
    }
};

class ClientStub : public CommonAPI::ClientId
{
public:
    ~ClientStub(){}
    bool operator==(ClientId& clientIdToCompare) {
        return true;
    }

    std::size_t hashCode() {
        return 0;
    }
};

class  MapViewerControlServerStub : public MapViewerControlStubDefault
{
	public:
#define MAX_SESSION_HANDLE 256
#define FIRST_SESSION_HANDLE 1

    MapViewerControlServerStub()
	{
        mp_handles[FIRST_SESSION_HANDLE]=NULL;
        m_version.setVersionMajor(3);
        m_version.setVersionMinor(0);
        m_version.setVersionMicro(0);
        m_version.setDate("21-01-2014");
    }

    /**
     * description: getVersion = This method returns the API version implemented by the server
     *   application
     */
    void getVersion(const std::shared_ptr<CommonAPI::ClientId> _client, getVersionReply_t _reply){
        _reply(m_version);
    }

    /**
     * description: createMapViewInstance = This method creates a new map instance
     */
    void createMapViewInstance(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, MapViewerControl::Dimension _mapViewSize, MapViewerControl::MapViewType _mapViewType, createMapViewInstanceReply_t _reply)
    {
        MapViewerControl::createMapViewInstanceError _error = MapViewerControl::createMapViewInstanceError::OK;
        NavigationTypes::Handle  _mapViewInstanceHandle=0;
        dbg(lvl_debug,"enter\n");
        if (_mapViewType != MapViewerControl::MapViewType::MAIN_MAP)
            _error = MapViewerControl::createMapViewInstanceError::MAPVIEWERCONTROL_ERROR_NOMOREMAPVIEWINSTANCEHANDLES;
        else {
            _mapViewInstanceHandle=FIRST_SESSION_HANDLE;
            while ((mp_handles.count(_mapViewInstanceHandle)>0 ) && (mp_handles[_mapViewInstanceHandle] != NULL)) {
                _mapViewInstanceHandle++;
                if (_mapViewInstanceHandle == MAX_SESSION_HANDLE)
                    _error = MapViewerControl::createMapViewInstanceError::MAPVIEWERCONTROL_ERROR_NOMOREMAPVIEWINSTANCEHANDLES;
            }
            mp_handles[_mapViewInstanceHandle]=new MapViewerControlObj(this, _mapViewInstanceHandle, _mapViewSize);
        }
        _reply(_error,_mapViewInstanceHandle);
	}

    /**
     * description: releaseMapViewInstance = This method releases (i.e. destroys) a given map
     *   instance. Only invisible map instances can be released
     */
    void releaseMapViewInstance(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, releaseMapViewInstanceReply_t _reply)
	{
        MapViewerControl::releaseMapViewInstanceError _error = MapViewerControl::releaseMapViewInstanceError::OK;
        std::map<uint32_t, MapViewerControlObj *>::iterator iter = mp_handles.find(_mapViewInstanceHandle);
        if (iter != mp_handles.end())
        {
            MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
            delete(obj); //delete the navit mapviewer instance
            //and remove the handle from the dictionary too
            mp_handles.erase(iter);
        }
        else {
           _error = MapViewerControl::releaseMapViewInstanceError::MAPVIEWERCONTROL_ERROR_MAPVIEWINSTANCENOTAVAILABLE;
        }

        _reply(_error);
	}

    /**
     * description: getMapViewType = This method returns the map type of a map instance as it was
     *   set using CreateMapViewInstance
     */
    void getMapViewType(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewTypeReply_t _reply){
        MapViewerControl::MapViewType _mapViewType=MapViewerControl::MapViewType::INVALID;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetMapViewType(_mapViewType);
        _reply(_mapViewType);

    }

    /**
     * description: getSupportedMapViewTypes = This method retrieves the supported map view types
     */
    void getSupportedMapViewTypes(const std::shared_ptr<CommonAPI::ClientId> _client, getSupportedMapViewTypesReply_t _reply){
        std::vector<MapViewerControl::MapViewType> _mapViewTypeList;
        _mapViewTypeList.push_back(MapViewerControl::MapViewType::MAIN_MAP);
        _reply(_mapViewTypeList);
    }

    /**
     * description: setTargetPoint = This method sets the position of the point the camera is
     *   always aimed at
     */
    void setTargetPoint(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, ::v4::org::genivi::navigation::NavigationTypes::Coordinate3D _targetPoint, setTargetPointReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetTargetPoint(_sessionHandle, _targetPoint);
        _reply();
    }

    /**
     * description: getTargetPoint = This method retrieves the target point position
     */
    void getTargetPoint(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getTargetPointReply_t _reply){
        NavigationTypes::Coordinate3D _targetPoint;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetTargetPoint(_targetPoint);
        _reply(_targetPoint);

    }

    /**
     * description: setFollowCarMode = This method sets the FollowCar mode
     */
    void setFollowCarMode(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, bool _followCarMode, setFollowCarModeReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetFollowCarMode(_sessionHandle, _followCarMode);
        fireFollowCarModeChangedEvent(_mapViewInstanceHandle,_followCarMode);
        _reply();
    }

    /**
     * description: getFollowCarMode = This method returns the current FollowCar-mode
     */
    void getFollowCarMode(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getFollowCarModeReply_t _reply){
        bool _followCarMode=false;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetFollowCarMode(_followCarMode);
        _reply(_followCarMode);

    }

    /**
     * description: setCameraPosition = This method sets the coordinates of the point at which the
     *   camera must be positioned
            Note: the camera heading will be automatically set
     *   in such a way, that the camera is aimed at the view point
     */
    void setCameraPosition(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, ::v4::org::genivi::navigation::NavigationTypes::Coordinate3D _position, setCameraPositionReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getCameraPosition = This method returns the coordinates of the point at which
     *   the camera is positioned
     */
    void getCameraPosition(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getCameraPositionReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setCameraHeadingAngle = This method sets the map view heading angle
            Note: the
     *   camera position will be automatically set in such a way, that it looks at the
     *   currently selected target point
     */
    void setCameraHeadingAngle(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, int32_t _heading, setCameraHeadingAngleReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetCameraHeadingAngle(_sessionHandle, _heading);
        _reply();
    }

    /**
     * description: setCameraHeadingToTarget = This method sets the camera heading in such a way,
     *   that the camera always looks at a given target
     */
    void setCameraHeadingToTarget(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, ::v4::org::genivi::navigation::NavigationTypes::Coordinate2D _target, setCameraHeadingToTargetReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setCameraHeadingTrackUp = This method sets the camera heading in such a way,
     *   that the camera always looks in the direction in which the car is moving
     */
    void setCameraHeadingTrackUp(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, setCameraHeadingTrackUpReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetCameraHeadingTrackUp(_sessionHandle);
        _reply();
    }

    /**
     * description: getCameraHeading = This method returns the current camera heading
     */
    void getCameraHeading(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getCameraHeadingReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setCameraTiltAngle = This method sets the camera tilt angle
     */
    void setCameraTiltAngle(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, int32_t _tilt, setCameraTiltAngleReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetCameraTiltAngle(_sessionHandle, _tilt);
        _reply();
    }

    /**
     * description: getCameraTiltAngle = This method returns the camera tilt angle
     */
    void getCameraTiltAngle(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getCameraTiltAngleReply_t _reply){
        int32_t _tilt;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetCameraTiltAngle(_tilt);
        _reply(_tilt);

    }

    /**
     * description: setCameraRollAngle = This method sets the camera roll angle
     */
    void setCameraRollAngle(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, int32_t _roll, setCameraRollAngleReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetCameraRollAngle(_sessionHandle, _roll);
        _reply();
    }

    /**
     * description: getCameraRollAngle = This method returns the camera roll angle
     */
    void getCameraRollAngle(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getCameraRollAngleReply_t _reply){
        int32_t _roll;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetCameraRollAngle(_roll);
        _reply(_roll);

    }

    /**
     * description: setCameraDistanceFromTargetPoint = This method sets the mode and the camera
     *   distance from the target point
     */
    void setCameraDistanceFromTargetPoint(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, uint32_t _distance, setCameraDistanceFromTargetPointReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetCameraDistanceFromTargetPoint(_sessionHandle, _distance);
        _reply();
    }

    /**
     * description: getCameraDistanceFromTargetPoint = This method gets the mode and the camera
     *   distance from the target point
     */
    void getCameraDistanceFromTargetPoint(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getCameraDistanceFromTargetPointReply_t _reply){
        uint32_t _distance=0;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetCameraDistanceFromTargetPoint(_distance);
        _reply(_distance);
    }

    /**
     * description: setMapViewScaleMode = This method sets the scaling mode.
     */
    void setMapViewScaleMode(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::MapScaleMode _scaleMode, setMapViewScaleModeReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getMapViewScaleMode = This method gets the scaling mode.
     */
    void getMapViewScaleMode(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewScaleModeReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getSupportedMapViewScaleModes = This method gets the supported scaling modes.
     */
    void getSupportedMapViewScaleModes(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getSupportedMapViewScaleModesReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setCameraHeight = This method sets the camera height
     */
    void setCameraHeight(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, uint32_t _height, setCameraHeightReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetCameraHeight(_sessionHandle, _height);
        _reply();
    }

    /**
     * description: getCameraHeight = This method gets the camera height
     */
    void getCameraHeight(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getCameraHeightReply_t _reply){
        uint32_t _height;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetCameraHeight(_height);
        _reply(_height);
    }

    /**
     * description: setMapViewPerspective = This method sets the map perspective
     */
    void setMapViewPerspective(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::MapPerspective _perspective, setMapViewPerspectiveReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetMapViewPerspective(_sessionHandle, _perspective);
        _reply();
    }

    /**
     * description: getMapViewPerspective = This method returns the current map perspective
     */
    void getMapViewPerspective(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewPerspectiveReply_t _reply){
        MapViewerControl::MapPerspective _perspective=MapViewerControl::MapPerspective::INVALID;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetMapViewPerspective(_perspective);
        _reply(_perspective);
    }

    /**
     * description: getSupportedMapViewPerspectives = This method retrieves the supported mapview
     *   perspectives
     */
    void getSupportedMapViewPerspectives(const std::shared_ptr<CommonAPI::ClientId> _client, getSupportedMapViewPerspectivesReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setMapViewObjectVisibility = This method specifies the type of objects to show
     *   on the map.
     */
    void setMapViewObjectVisibility(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::MapObjectVisibility _objectVisibilityList, setMapViewObjectVisibilityReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getMapViewObjectVisibility = This method gets the type of objects shown on the
     *   map.
     */
    void getMapViewObjectVisibility(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewObjectVisibilityReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getSupportedMapViewObjectVisibilities = This method gets the supported object
     *   visibilities.
     */
    void getSupportedMapViewObjectVisibilities(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getSupportedMapViewObjectVisibilitiesReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getScaleList = This method returns a list of supported map scales
     */
    void getScaleList(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getScaleListReply_t _reply){
        std::vector<MapViewerControl::MapScale> _scaleList;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetScaleList(_scaleList);
        _reply(_scaleList);
    }

    /**
     * description: setMapViewScale = This method sets the map scale by specifying a ScaleID
     */
    void setMapViewScale(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, uint8_t _scaleID, setMapViewScaleReply_t _reply){
        MapViewerControl::MapScaleType is_min_max=MapViewerControl::MapScaleType::INVALID;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else  {
//            obj->SetMapViewScale(_sessionHandle, _scaleID);
//            obj->GetMapViewScale(_scaleID,is_min_max);
            fireMapViewScaleChangedSelective(_mapViewInstanceHandle,_scaleID,is_min_max);
            fireFollowCarModeChangedEvent(_mapViewInstanceHandle,false);

        }
        _reply();
    }

    /**
     * description: setMapViewScaleByDelta = This method sets the map scale by specifying a delta
     *   value with respect to the currently set ScaleID
     */
    void setMapViewScaleByDelta(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, int16_t _scaleDelta, setMapViewScaleByDeltaReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else {
            obj->SetMapViewScaleByDelta(_sessionHandle, _scaleDelta);
            uint8_t current_scale;
            MapViewerControl::MapScaleType is_min_max;
            obj->GetMapViewScale(current_scale,is_min_max);
            fireMapViewScaleChangedSelective(_mapViewInstanceHandle,current_scale,is_min_max);
        }
        _reply();
    }

    /**
     * description: setMapViewScaleByMetersPerPixel = This method sets the map scale by specifying
     *   the number of meters that a pixel represents
     */
    void setMapViewScaleByMetersPerPixel(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, double _metersPerPixel, setMapViewScaleByMetersPerPixelReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getMapViewScale = This method returns the currently used map scale
     */
    void getMapViewScale(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewScaleReply_t _reply){
        uint8_t _scaleID=0;
        MapViewerControl::MapScaleType _isMinMax=MapViewerControl::MapScaleType::INVALID;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetMapViewScale(_scaleID, _isMinMax);
        _reply(_scaleID,_isMinMax);
    }

    /**
     * description: setMapViewBoundingBox = This method sets the map bounding box
     */
    void setMapViewBoundingBox(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, ::v4::org::genivi::navigation::NavigationTypes::Rectangle _boundingBox, setMapViewBoundingBoxReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetMapViewBoundingBox(_sessionHandle, _boundingBox);
        _reply();
    }

    /**
     * description: getMapViewBoundingBox = This method returns the bounding box of a given map
     *   instance
     */
    void getMapViewBoundingBox(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewBoundingBoxReply_t _reply){
        NavigationTypes::Rectangle _boundingBox;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetMapViewBoundingBox(_boundingBox);
        _reply(_boundingBox);
    }

    /**
     * description: setMapViewSaveArea = This methods defines the area that the HMI guarantees not
     *   to cover with other windows or user interface elements
     */
    void setMapViewSaveArea(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::MapViewArea _saveArea, setMapViewSaveAreaReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getMapViewSaveArea = This methods defines the area that the HMI guarantees not
     *   to cover with other windows or user interface elements
     */
    void getMapViewSaveArea(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewSaveAreaReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setMapViewPan = This method pans a given map instance
     */
    void setMapViewPan(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::PanAction _panningAction, MapViewerControl::Pixel _pixelCoordinates, setMapViewPanReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else {
                obj->SetMapViewPan(_sessionHandle, _panningAction, _pixelCoordinates);
        }
        _reply();
    }

    /**
     * description: getMapViewPan
     */
    void getMapViewPan(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::PanAction _valueToReturn, getMapViewPanReply_t _reply){
        MapViewerControl::Pixel _pixelCoordinates;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetMapViewPan(_valueToReturn, _pixelCoordinates); //limited to one pixel coordinate
        _reply(_pixelCoordinates);
    }

    /**
     * description: setMapViewRotation = This method rotates the map
     */
    void setMapViewRotation(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, int32_t _rotationAngle, int32_t _rotationAnglePerSecond, setMapViewRotationReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetMapViewRotation(_sessionHandle, _rotationAngle, _rotationAnglePerSecond);
        _reply();
    }

    /**
     * description: getMapViewRotation = This method is particularly interesting for debugging
     *   purposes
     */
    void getMapViewRotation(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewRotationReply_t _reply){
        int32_t _rotationAngle=0;
        int32_t _rotationAnglePerSecond=0;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetMapViewRotation(_rotationAngle, _rotationAnglePerSecond);
        _reply(_rotationAngle,_rotationAnglePerSecond);
    }

    /**
     * description: setMapViewVisibilityMode = This method sets the current visibility mode
     */
    void setMapViewVisibilityMode(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::Visibility _visibilityMode, setMapViewVisibilityModeReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getMapViewVisibilityMode = This method returns the current visibility mode
     */
    void getMapViewVisibilityMode(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewVisibilityModeReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getSupportedMapViewVisibilityModes = This method retrieves the supported
     *   mapview visibility modes
     */
    void getSupportedMapViewVisibilityModes(const std::shared_ptr<CommonAPI::ClientId> _client, getSupportedMapViewVisibilityModesReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setMapViewPerformanceLevel = This method sets the perfomance level of a given
     *   map instance
     */
    void setMapViewPerformanceLevel(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::Level _performanceLevel, setMapViewPerformanceLevelReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getMapViewPerformanceLevel = This method returns the perfomance level of a
     *   given map instance
     */
    void getMapViewPerformanceLevel(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewPerformanceLevelReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getSupportedMapViewPerformanceLevels = This method retrieves the supported
     *   perfomance levels
     */
    void getSupportedMapViewPerformanceLevels(const std::shared_ptr<CommonAPI::ClientId> _client, getSupportedMapViewPerformanceLevelsReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: displayRoute = This method visualizes one of the calculated routes
     */
    void displayRoute(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, bool _highlighted, displayRouteReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->DisplayRoute(_sessionHandle, _routeHandle, _highlighted);
        _reply();
    }

    /**
     * description: hideRoute = This method hides one of the visible routes
     */
    void hideRoute(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, hideRouteReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->HideRoute(_sessionHandle, _routeHandle);
        _reply();
    }

    /**
     * description: getDisplayedRoutes = This method returns a list of displayed routes
     */
    void getDisplayedRoutes(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getDisplayedRoutesReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        std::vector<MapViewerControl::DisplayedRoute> _displayedRoutes;
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetDisplayedRoutes(_displayedRoutes);
        _reply(_displayedRoutes);
    }

    /**
     * description: getPoiCategoriesVisible = Get the set of POI categories displayed on the map.
     */
    void getPoiCategoriesVisible(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getPoiCategoriesVisibleReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setPoiCategoriesVisible = Add POI categories to the set of POI categories
     *   displayed on the map.
                       Any specified category that until now
     *   was displayed with scale limits is now displayed without limits.
     */
    void setPoiCategoriesVisible(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, std::vector< ::v4::org::genivi::CommonTypes::CategoryID> _poiCategoryIds, setPoiCategoriesVisibleReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setPoiCategoriesVisible = Add POI categories to the set of POI categories
     *   displayed on the map, where the POI's are only displayed in a specific range
     *   of scales.
                       Any specified category that until now was
     *   displayed without scale limits is now displayed with limits.
     */
    void setPoiCategoriesVisibleWithinLimits(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, std::vector< ::v4::org::genivi::CommonTypes::CategoryID> _poiCategoryIds, uint8_t _minScaleID, uint8_t _maxScaleID, setPoiCategoriesVisibleWithinLimitsReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setPoiCategoriesNotVisible = Remove POI categories from the set of POI
     *   categories displayed on the map.
     */
    void setPoiCategoriesNotVisible(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, std::vector< ::v4::org::genivi::CommonTypes::CategoryID> _poiCategoryIds, setPoiCategoriesNotVisibleReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setTrafficIncidentsVisibility = Set the visibility of Traffic Incidents on the
     *   map.
     */
    void setTrafficIncidentsVisibility(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, bool _visible, setTrafficIncidentsVisibilityReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setMapViewTheme = This method configures the theme of a given map view instance
     */
    void setMapViewTheme(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::MapTheme _mapViewTheme, setMapViewThemeReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->SetMapViewTheme(_sessionHandle, _mapViewTheme);
        _reply();
    }

    /**
     * description: getMapViewTheme = This method returns the current theme of a given map view
     *   instance
     */
    void getMapViewTheme(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getMapViewThemeReply_t _reply){
        MapViewerControl::MapTheme _mapViewTheme=MapViewerControl::MapTheme::INVALID;
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->GetMapViewTheme(_mapViewTheme);
        _reply(_mapViewTheme);

    }

    /**
     * description: getSupportedMapViewThemes = This method retrieves the supported mapview themes
     */
    void getSupportedMapViewThemes(const std::shared_ptr<CommonAPI::ClientId> _client, getSupportedMapViewThemesReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: convertPixelCoordsToGeoCoords = This method converts pixel coordinates to
     *   geographical coordinates
     */
    void convertPixelCoordsToGeoCoords(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, std::vector<MapViewerControl::Pixel> _pixelCoordinates, convertPixelCoordsToGeoCoordsReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        std::vector< ::v4::org::genivi::navigation::NavigationTypes::Coordinate2D> _geoCoordinates;
        MapViewerControl::convertPixelCoordsToGeoCoordsError _error = MapViewerControl::convertPixelCoordsToGeoCoordsError::OK;
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->ConvertPixelCoordsToGeoCoords(_sessionHandle, _pixelCoordinates, _geoCoordinates);
        _reply(_error,_geoCoordinates);
    }

    /**
     * description: convertGeoCoordsToPixelCoords = This method converts geographical coordinates
     *   into pixel coordinates
     */
    void convertGeoCoordsToPixelCoords(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, std::vector< ::v4::org::genivi::navigation::NavigationTypes::Coordinate2D> _geoCoordinates, convertGeoCoordsToPixelCoordsReply_t _reply){
        MapViewerControlObj *obj=mp_handles[_mapViewInstanceHandle];
        std::vector<MapViewerControl::Pixel> _pixelCoordinates;
        MapViewerControl::convertGeoCoordsToPixelCoordsError _error = MapViewerControl::convertGeoCoordsToPixelCoordsError::OK;
        if (!obj)
            throw DBus::ErrorInvalidArgs("Invalid mapviewinstance handle");
        else obj->ConvertGeoCoordsToPixelCoords(_sessionHandle, _geoCoordinates, _pixelCoordinates);
        _reply(_error,_pixelCoordinates);
    }

    /**
     * description: displayCustomElements = This method visualizes a set of custom elements on the
     *   map
     */
    void displayCustomElements(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, std::vector<MapViewerControl::CustomElement> _customElements, displayCustomElementsReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: hideCustomElements = This method hides a set of custom elements which were
     *   visualized by DisplayCustomElements
     */
    void hideCustomElements(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, std::vector< ::v4::org::genivi::navigation::NavigationTypes::Handle> _customElementhandles, hideCustomElementsReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getDisplayedCustomElements = This method retrieves the visualized custom
     *   elements on the map
     */
    void getDisplayedCustomElements(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, getDisplayedCustomElementsReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: selectElementsOnMap = This method selects elements on the map view which are at
     *   the position specified by user input
     */
    void selectElementsOnMap(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _mapViewInstanceHandle, MapViewerControl::Pixel _pixelCoordinate, std::vector<MapViewerControl::SelectableMapType> _selectableTypes, uint16_t _maxNumberOfSelectedElements, selectElementsOnMapReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

private:
    CommonTypes::Version m_version;

};

void
MapViewerControlObj::SetMapViewPerspective(NavigationTypes::Handle SessionHandle, MapViewerControl::MapPerspective MapViewPerspectiveMode)
{
	if (m_perspective != MapViewPerspectiveMode) {
		m_perspective=MapViewPerspectiveMode;
		struct transformation *trans=navit_get_trans(m_navit.u.navit);
		switch (m_perspective) {
        case MapViewerControl::MapPerspective::PERSPECTIVE_2D:
			transform_set_pitch(trans, 0);
			break;
        case MapViewerControl::MapPerspective::PERSPECTIVE_3D:
			transform_set_pitch(trans, 40);
			break;
		}
		navit_draw_async(m_navit.u.navit, 1);
	}
}

void
MapViewerControlObj::GetMapViewPerspective(MapViewerControl::MapPerspective& MapViewPerspectiveMode)
{
	MapViewPerspectiveMode=m_perspective;
}

void
MapViewerControlObj::GetScaleList(std::vector<MapViewerControl::MapScale> &ScalesList)
{
    MapViewerControl::MapScale scale;
    uint32_t basisPixel = ZOOM_ZERO_METER_WIDTH/m_width;

    scale.setScaleId(0);
    scale.setScaleValue(2);
    scale.setUnit(MapViewerControl::MapScaleUnit::METER);
    scale.setMillimetersPerPixel(basisPixel);
    ScalesList.push_back(scale);
    scale.setScaleId(1);
    scale.setScaleValue(5);
    scale.setUnit(MapViewerControl::MapScaleUnit::METER);
    scale.setMillimetersPerPixel(basisPixel<<1);
    ScalesList.push_back(scale);
    scale.setScaleId(2);
    scale.setScaleValue(10);
    scale.setUnit(MapViewerControl::MapScaleUnit::METER);
    scale.setMillimetersPerPixel(basisPixel<<2);
    ScalesList.push_back(scale);
    scale.setScaleId(3);
    scale.setScaleValue(20);
    scale.setUnit(MapViewerControl::MapScaleUnit::METER);
    scale.setMillimetersPerPixel(basisPixel<<3);
    ScalesList.push_back(scale);
    scale.setScaleId(4);
    scale.setScaleValue(50);
    scale.setUnit(MapViewerControl::MapScaleUnit::METER);
    scale.setMillimetersPerPixel(basisPixel<<4);
    ScalesList.push_back(scale);
    scale.setScaleId(5);
    scale.setScaleValue(100);
    scale.setUnit(MapViewerControl::MapScaleUnit::METER);
    scale.setMillimetersPerPixel(basisPixel<<5);
    ScalesList.push_back(scale);
    scale.setScaleId(6);
    scale.setScaleValue(200);
    scale.setUnit(MapViewerControl::MapScaleUnit::METER);
    scale.setMillimetersPerPixel(basisPixel<<6);
    ScalesList.push_back(scale);
    scale.setScaleId(7);
    scale.setScaleValue(500);
    scale.setUnit(MapViewerControl::MapScaleUnit::METER);
    scale.setMillimetersPerPixel(basisPixel<<7);
    ScalesList.push_back(scale);
    scale.setScaleId(8);
    scale.setScaleValue(1);
    scale.setUnit(MapViewerControl::MapScaleUnit::KM);
    scale.setMillimetersPerPixel(basisPixel<<8);
    ScalesList.push_back(scale);
    scale.setScaleId(9);
    scale.setScaleValue(2);
    scale.setUnit(MapViewerControl::MapScaleUnit::KM);
    scale.setMillimetersPerPixel(basisPixel<<9);
    ScalesList.push_back(scale);
    scale.setScaleId(10);
    scale.setScaleValue(5);
    scale.setUnit(MapViewerControl::MapScaleUnit::KM);
    scale.setMillimetersPerPixel(basisPixel<<10);
    ScalesList.push_back(scale);
    scale.setScaleId(11);
    scale.setScaleValue(10);
    scale.setUnit(MapViewerControl::MapScaleUnit::KM);
    scale.setMillimetersPerPixel(basisPixel<<11);
    ScalesList.push_back(scale);
    scale.setScaleId(12);
    scale.setScaleValue(20);
    scale.setUnit(MapViewerControl::MapScaleUnit::KM);
    scale.setMillimetersPerPixel(basisPixel<<12);
    ScalesList.push_back(scale);
    scale.setScaleId(13);
    scale.setScaleValue(50);
    scale.setUnit(MapViewerControl::MapScaleUnit::KM);
    scale.setMillimetersPerPixel(basisPixel<<13);
    ScalesList.push_back(scale);
    scale.setScaleId(14);
    scale.setScaleValue(100);
    scale.setUnit(MapViewerControl::MapScaleUnit::KM);
    scale.setMillimetersPerPixel(basisPixel<<14);
    ScalesList.push_back(scale);
    scale.setScaleId(15);
    scale.setScaleValue(200);
    scale.setUnit(MapViewerControl::MapScaleUnit::KM);
    scale.setMillimetersPerPixel(basisPixel<<15);
    ScalesList.push_back(scale);
}

void
MapViewerControlObj::SetMapViewScale(NavigationTypes::Handle SessionHandle, uint8_t ScaleID)
{
	long scale=1 << ScaleID;
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	transform_set_scale(trans, scale);
    navit_draw(m_navit.u.navit);
}

void
MapViewerControlObj::SetMapViewScaleByDelta(NavigationTypes::Handle SessionHandle, int16_t ScaleDelta)
{
    if (ScaleDelta < 0)
        navit_zoom_in(m_navit.u.navit,1 << (-ScaleDelta),NULL);
    else if (ScaleDelta > 0)
        navit_zoom_out(m_navit.u.navit,1 << ScaleDelta,NULL);
}

void
MapViewerControlObj::GetMapViewScale(uint8_t& ScaleID, MapViewerControl::MapScaleType &IsMinMax)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	long scale=transform_get_scale(trans);
    if (scale <= 1)
        IsMinMax=MapViewerControl::MapScaleType::MIN;
    else
        if (scale >= 2097152)
            IsMinMax=MapViewerControl::MapScaleType::MAX;
        else
            IsMinMax=MapViewerControl::MapScaleType::MID;
    ScaleID=0;
    while (scale > 1) {
        scale >>=1;
        ScaleID++;
    }
}

void
MapViewerControlObj::GetMapViewType(MapViewerControl::MapViewType& MapViewType)
{
    MapViewType=MapViewerControl::MapViewType::MAIN_MAP;
}

void
MapViewerControlObj::SetMapViewTheme(NavigationTypes::Handle SessionHandle, MapViewerControl::MapTheme mapViewTheme)
{
	struct attr layout,name;
	struct attr_iter *iter;
	const char *layout_name=NULL;
    dbg(lvl_debug,"Theme %d\n",mapViewTheme);
	switch (mapViewTheme) {
    case MapViewerControl::MapTheme::THEME_1:
		layout_name="Car";
		break;
    case MapViewerControl::MapTheme::THEME_2:
		layout_name="Car-dark";
		break;
	default:
        dbg(lvl_debug,"Invalid mapViewTheme\n");
		throw DBus::ErrorInvalidArgs("Invalid mapViewTheme");
	}
	iter=navit_attr_iter_new();
	while (navit_get_attr(m_navit.u.navit, attr_layout, &layout, iter)) {
        dbg(lvl_debug,"layout\n");
		if (!layout_get_attr(layout.u.layout, attr_name, &name, NULL)) {
			navit_attr_iter_destroy(iter);
			throw DBus::ErrorFailed("Internal error: Failed to get layout name");
		}
        dbg(lvl_debug,"%s vs %s\n",name.u.str,layout_name);
		if (!strcmp(name.u.str,layout_name)) {
			navit_set_attr(m_navit.u.navit, &layout);
			navit_attr_iter_destroy(iter);
			return;
		}
	}
	navit_attr_iter_destroy(iter);
	throw DBus::ErrorFailed("Internal error: Failed to get map layout");
}

void
MapViewerControlObj::GetMapViewTheme(MapViewerControl::MapTheme& mapViewTheme)
{
	struct attr layout,name;
	if (!navit_get_attr(m_navit.u.navit, attr_layout, &layout, NULL))
		throw DBus::ErrorFailed("Internal error: Failed to get map layout");
	if (!layout_get_attr(layout.u.layout, attr_name, &name, NULL)) 
		throw DBus::ErrorFailed("Internal error: Failed to get layout name");
    dbg(lvl_debug,"name %s\n",name.u.str);
	if (!strcmp(name.u.str,"Car-dark"))
        mapViewTheme=MapViewerControl::MapTheme::THEME_2;
	else
        mapViewTheme=MapViewerControl::MapTheme::THEME_1;
}

void
MapViewerControlObj::SetFollowCarMode(NavigationTypes::Handle SessionHandle, bool active)
{
	struct attr follow={attr_follow};
	m_follow_car=active;
	if (active)
		follow.u.num=1;
	else
		follow.u.num=100000;
    dbg(lvl_debug,"setting follow to %d\n",(int) follow.u.num);
	navit_set_attr(m_navit.u.navit,&follow);
	vehicle_set_attr(m_vehicle.u.vehicle,&follow);
	follow.type=attr_timeout;
	navit_set_attr(m_navit.u.navit,&follow);
}

void
MapViewerControlObj::GetFollowCarMode(bool& active)
{
	active=m_follow_car;
}

void
MapViewerControlObj::SetCameraHeadingAngle(NavigationTypes::Handle SessionHandle, double angle)
{
	struct attr orientation={attr_orientation};
	orientation.u.num=angle;
	navit_set_attr(m_navit.u.navit, &orientation);
	navit_draw_async(m_navit.u.navit, 1);
}

void
MapViewerControlObj::GetCameraHeadingAngle(double &angle)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	angle=transform_get_yaw(trans);
}

void
MapViewerControlObj::SetCameraTiltAngle(NavigationTypes::Handle SessionHandle, double angle)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	transform_set_pitch(trans, angle);
	navit_draw(m_navit.u.navit);
	
}

void
MapViewerControlObj::GetCameraTiltAngle(int32_t &angle)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	angle=transform_get_pitch(trans);
}

void
MapViewerControlObj::SetCameraRollAngle(NavigationTypes::Handle SessionHandle, double angle)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	transform_set_roll(trans, angle);
	navit_draw(m_navit.u.navit);
}

void
MapViewerControlObj::GetCameraRollAngle(int32_t &angle)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	angle=transform_get_roll(trans);
}

void
MapViewerControlObj::SetCameraDistanceFromTargetPoint(NavigationTypes::Handle SessionHandle, double distance)
{
	struct transformation *t=navit_get_trans(m_navit.u.navit);
	transform_set_distance(t, distance);
    dbg(lvl_debug,"distance %f\n",distance);
	transform_set_scales(t, 100, 100, 100 << 8);
	navit_draw(m_navit.u.navit);
}

void
MapViewerControlObj::GetCameraDistanceFromTargetPoint(uint32_t &distance)
{
	struct transformation *t=navit_get_trans(m_navit.u.navit);
	distance=transform_get_distance(t);
}

void
MapViewerControlObj::SetCameraHeight(NavigationTypes::Handle SessionHandle, double height)
{
	struct transformation *t=navit_get_trans(m_navit.u.navit);
	double distance=transform_get_distance(t);
	if (height > distance || height < -distance) 
		throw DBus::ErrorInvalidArgs("Height > Distance");
	double angle=acos(height/distance)*180.0/M_PI;
	transform_set_pitch(t, angle);
    dbg(lvl_debug,"distance %f angle %f height %f\n",distance,angle,height);
	navit_draw(m_navit.u.navit);
}

void
MapViewerControlObj::GetCameraHeight(uint32_t &height)
{
	struct transformation *t=navit_get_trans(m_navit.u.navit);
	double distance=transform_get_distance(t);
	double angle=transform_get_pitch(t);
	height=cos(angle*M_PI/180)*distance;
    dbg(lvl_debug,"distance %f angle %f height %f\n",distance,angle,height);
}

void
MapViewerControlObj::SetCameraHeadingTrackUp(NavigationTypes::Handle SessionHandle)
{
	struct attr orientation={attr_orientation};
	orientation.u.num=-1;
	navit_set_attr(m_navit.u.navit, &orientation);
	navit_draw_async(m_navit.u.navit, 1);
}

void
MapViewerControlObj::SetTargetPoint(NavigationTypes::Handle SessionHandle, NavigationTypes::Coordinate3D target)
{
	struct coord_geo g;
	struct attr center={attr_center};
	center.u.coord_geo=&g;
    g.lat=target.getLatitude();
    g.lng=target.getLongitude();
	navit_set_attr(m_navit.u.navit, &center);
	navit_draw_async(m_navit.u.navit, 1);
}

void
MapViewerControlObj::GetTargetPoint(NavigationTypes::Coordinate3D&target)
{
	struct attr center;
	if (!navit_get_attr(m_navit.u.navit, attr_center, &center, NULL) || !center.u.coord_geo) 
		throw DBus::ErrorFailed("Internal error: Failed to get center attribute");
    target.setLatitude(center.u.coord_geo->lat);
    target.setLongitude(center.u.coord_geo->lng);
    target.setAltitude(0);
}

void
MapViewerControlObj::MoveMap(void)
{
        if (m_scrollspeed || m_rotationanglepersecond || m_force_draw) {
		int w,h;
		double refresh_time=0.3; // Time needed to redraw map
		double r=m_scrollspeed*refresh_time;
		struct point p;
		struct transformation *t=navit_get_trans(m_navit.u.navit);
                if (m_rotationanglepersecond) {
			int yaw=transform_get_yaw(t);
			int delta=((int)(m_rotationangle+0.5)-yaw)%360;
			if (delta > 180)
				delta-=180;
			if (delta < -180)
				delta+=180;
                        if (delta < 0 && delta < -m_rotationanglepersecond)
                                delta=-m_rotationanglepersecond;
                        if (delta > 0 && delta > m_rotationanglepersecond)
                                delta=m_rotationanglepersecond;
			if (delta)
				transform_set_yaw(t, yaw+delta);
			else
                                m_rotationanglepersecond=0;
		}
		transform_get_size(t, &w, &h);
		p.x=w/2+sin(m_scrolldirection*M_PI/180)*r;
		p.y=h/2-cos(m_scrolldirection*M_PI/180)*r;
		navit_set_center_screen(m_navit.u.navit, &p, 1);
		m_force_draw=false;
	}
}

static void
MapViewerControlObj_MoveMap(class MapViewerControlObj *obj)
{
	obj->MoveMap();
}

static void
MapViewerControlObj_PostDraw(class MapViewerControlObj *obj)
{
	event_add_timeout(0, 0, obj->m_move_callback);
}

void
MapViewerControlObj::SetMapViewPan(NavigationTypes::Handle SessionHandle, MapViewerControl::PanAction panningAction, MapViewerControl::Pixel p)
{
    struct transformation *tr;
    struct coord co,cn,c,*cp;
    struct point pan;
    SetFollowCarMode(SessionHandle, false);
    dbg(lvl_debug,"enter %d\n",panningAction);
    switch(panningAction) {
    case MapViewerControl::PanAction::PAN_START:
	break;
    case MapViewerControl::PanAction::PAN_END:
	tr=navit_get_trans(m_navit.u.navit);
	transform_reverse(tr, &m_pan, &co);
    pan.x=p.getX();
    pan.y=p.getY();
	transform_reverse(tr, &pan, &cn);
    dbg(lvl_debug,"%d,%d - %d,%d\n",m_pan.x,m_pan.y,pan.x,pan.y);
	cp=transform_get_center(tr);
	c.x=cp->x+co.x-cn.x;
	c.y=cp->y+co.y-cn.y;
	transform_set_center(tr, &c);
	navit_draw(m_navit.u.navit);
    default:
	return;
    }
    m_pan_action=panningAction;
    m_pan.x=p.getX();
    m_pan.y=p.getY();
}

void
MapViewerControlObj::GetMapViewPan(const int32_t& panningAction, MapViewerControl::Pixel& p)
{
    p.setX(m_pan.x);
    p.setY(m_pan.y);
}

void
MapViewerControlObj::SetMapViewRotation(NavigationTypes::Handle SessionHandle, double rotationAngle, double rotationAnglePerSecond)
{
	m_rotationangle=rotationAngle;
        m_rotationanglepersecond=rotationAnglePerSecond;
    SetFollowCarMode(SessionHandle, false);
	MoveMap();

}

void
MapViewerControlObj::GetMapViewRotation(int32_t& rotationAngle, int32_t& _rotationAnglePerSecond)
{
	rotationAngle=m_rotationangle;
        _rotationAnglePerSecond=m_rotationanglepersecond;
}

void
MapViewerControlObj::SetMapViewBoundingBox(NavigationTypes::Handle SessionHandle, const NavigationTypes::Rectangle& boundingBox)
{
	struct coord_rect r;
	struct coord_geo g;
    SetFollowCarMode(SessionHandle, false);
    g.lat=boundingBox.getTopLeft().getLatitude();
    g.lng=boundingBox.getTopLeft().getLongitude();
    transform_from_geo(projection_mg, &g, &r.lu);
    g.lat=boundingBox.getBottomRight().getLatitude();
    g.lng=boundingBox.getBottomRight().getLongitude();
    transform_from_geo(projection_mg, &g, &r.rl);
    dbg(lvl_debug,"0x%x,0x%x-0x%x,0x%x\n",r.lu.x,r.lu.y,r.rl.x,r.rl.y);
    navit_zoom_to_rect(m_navit.u.navit, &r);
}

void
MapViewerControlObj::GetMapViewBoundingBox(NavigationTypes::Rectangle& boundingBox)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	struct point p;
	struct coord c;
	struct coord_geo g;
	int w,h;
    NavigationTypes::Coordinate2D coordinate;

    transform_get_size(trans, &w, &h);
	p.x=0;
	p.y=0;
	transform_reverse(trans, &p, &c);
	transform_to_geo(projection_mg, &c, &g);
    coordinate.setLatitude(g.lat);
    coordinate.setLongitude(g.lng);
    boundingBox.setTopLeft(coordinate);
	p.x=w;
	p.y=h;
	transform_reverse(trans, &p, &c);
	transform_to_geo(projection_mg, &c, &g);
    coordinate.setLatitude(g.lat);
    coordinate.setLongitude(g.lng);
    boundingBox.setBottomRight(coordinate);
}

void
MapViewerControlObj::GetDisplayedRoutes(std::vector<MapViewerControl::DisplayedRoute>& displayedRoutes)
{
    size_t i;
	for (i = 0 ; i < m_displayed_routes.size(); i++) {
        MapViewerControl::DisplayedRoute route;
        route.setRouteHandle(m_displayed_routes[i]->RouteHandle());
        route.setHighlighted(false);
		displayedRoutes.push_back(route);
	}
}

void
MapViewerControlObj::DisplayRoute(NavigationTypes::Handle SessionHandle, NavigationTypes::Handle RouteHandle, bool highlighted)
{
	HideRoute(SessionHandle, RouteHandle);

    if (m_navigationcore_session == NavigationTypes::BasicEnum::INVALID)
    {
        CommonAPI::CallStatus status;
        Session::createSessionError _error;
        mp_navigationCoreSessionClientProxy->myServiceNavigationCoreSession->createSession(std::string("MapViewerControl"),status,_error, m_navigationcore_session);
    }

    DisplayedRoute *route=new DisplayedRoute(this,m_navigationcore_session,RouteHandle,m_mapset);
	route->Show();
	m_displayed_routes.push_back(route);
	m_force_draw=true;
	event_add_timeout(0, 0, m_move_callback);
}

void
MapViewerControlObj::HideRoute(NavigationTypes::Handle SessionHandle, NavigationTypes::Handle RouteHandle)
{
	std::vector<DisplayedRoute *>::iterator it;

	for (it=m_displayed_routes.begin() ; it < m_displayed_routes.end(); it++ ) {
		if (*it && (*it)->m_handle == RouteHandle) {
			(*it)->Hide();
			m_displayed_routes.erase(it);
			delete(*it);
			m_force_draw=true;
			event_add_timeout(0, 0, m_move_callback);
		}
	}
    dbg(lvl_debug,"Route not displayed\n");
}

void
MapViewerControlObj::ConvertPixelCoordsToGeoCoords(NavigationTypes::Handle SessionHandle, const std::vector< MapViewerControl::Pixel >& pixelCoordinates, std::vector< NavigationTypes::Coordinate2D >& geoCoordinates)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	struct coord c;
	struct point p;
	struct coord_geo g;
    std::vector< MapViewerControl::Pixel >::const_iterator it;
	for (it=pixelCoordinates.begin(); it < pixelCoordinates.end(); it++) {
        NavigationTypes::Coordinate2D geoCoordinate;
        p.x=it->getX();
        p.y=it->getY();
		transform_reverse(trans, &p, &c);
		transform_to_geo(projection_mg, &c, &g);
        geoCoordinate.setLatitude(g.lat);
        geoCoordinate.setLongitude(g.lng);
		geoCoordinates.push_back(geoCoordinate);
	}
	
}

void
MapViewerControlObj::ConvertGeoCoordsToPixelCoords(NavigationTypes::Handle SessionHandle, const std::vector< NavigationTypes::Coordinate2D >& geoCoordinates, std::vector< MapViewerControl::Pixel >& pixelCoordinates)
{
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	struct coord c;
	struct point p;
	struct coord_geo g;
    std::vector< NavigationTypes::Coordinate2D >::const_iterator it;
	for (it=geoCoordinates.begin(); it < geoCoordinates.end(); it++) {
        MapViewerControl::Pixel pixelCoordinate;
        g.lat=it->getLatitude();
        g.lng=it->getLongitude();
		transform_from_geo(projection_mg, &g, &c);
		transform(trans, projection_mg, &c, &p, 1, 0, 0, NULL);
        pixelCoordinate.setX(p.x);
        pixelCoordinate.setY(p.y);
		pixelCoordinates.push_back(pixelCoordinate);
	}
}

#if LM
static void callbackFunction(ilmObjectType object, t_ilm_uint surfaceId, t_ilm_bool created, void *user_data)
{
    (void)user_data;

    t_ilm_int pLength = 0;
    t_ilm_layer* ppArray = NULL;
    struct ilmSurfaceProperties pSurfaceProperties;
    t_ilm_layer renderOrder[2];

    if (object == ILM_SURFACE) {
        if (created) {
            if (surfaceId == FSA_SURFACE) {

                //FIXIT: Need to add tempo
                sleep(1);

                //Grab all the layers - HMI will be pLength-2 and FSA pLength-1
                if (ilm_getLayerIDs(&pLength, &ppArray)  != ILM_SUCCESS) {
                    fprintf(stderr, "error on ilm_getLayerIDs\n");
                }

		//FIXIT: Since we don't know thep id of hmi_launcher and order of start.
                //Guess it is teh last one or the one before navit mapviewer PID.
                renderOrder[0] = FSA_SURFACE;
                if (ppArray[pLength - 2] != FSA_SURFACE)
                    renderOrder[1] = SURFACE_OFFSET + ppArray[pLength - 2];
                    if (ilm_layerSetRenderOrder(ppArray[pLength - 2],renderOrder,2) != ILM_SUCCESS) {
                        fprintf(stderr,"error on ilm_layerSetRenderOrder\n");
                    }
                else if (ppArray[pLength - 1] != FSA_SURFACE)
                    renderOrder[1] = SURFACE_OFFSET + ppArray[pLength - 1];
                    if (ilm_layerSetRenderOrder(ppArray[pLength - 1],renderOrder,2) != ILM_SUCCESS) {
                        fprintf(stderr,"error on ilm_layerSetRenderOrder\n");
                    }
                else
                     fprintf(stderr,"error on render order guess\n");

                if (ilm_layerSetRenderOrder(ppArray[pLength - 1],renderOrder,2) != ILM_SUCCESS) {
                    fprintf(stderr,"error on ilm_layerSetRenderOrder\n");
                }

                if (ilm_commitChanges() != ILM_SUCCESS) {
                    fprintf(stderr,"error on ilm_commitChanges\n");
                }
            }
	}
    }
}
#endif

class  MapMatchedPositionClientProxy;
class RoutingClientProxy;

MapViewerControlObj::MapViewerControlObj(MapViewerControlServerStub *mapviewercontrol, NavigationTypes::Handle handle, const MapViewerControl::Dimension &MapViewSize)
{
	mp_mapviewercontrol=mapviewercontrol;
	m_handle=handle;
	m_navit.type=attr_navit;
	m_scrollspeed=0;
	m_scrolldirection=0;
	m_force_draw=false;
    m_perspective=MapViewerControl::MapPerspective::PERSPECTIVE_2D;
	m_follow_car=true;
    m_navigationcore_session = NavigationTypes::BasicEnum::INVALID;
    //connect the routing client to map viewer
    mp_routingClientProxy->connectToMapViewer(this);
    struct attr navit_template;
	struct attr navit_flags={attr_flags};navit_flags.u.num=2;
	struct attr *navit_attrs[]={&navit_flags,NULL};
	if (!config_get_attr(config, attr_navit, &navit_template, NULL)) {
        dbg(lvl_debug,"failed to get navit template from config\n");
		return;
	}
	m_navit.u.navit=navit_new(NULL,navit_attrs);
	if (!m_navit.u.navit) {
        dbg(lvl_debug,"failed to create new navit instance\n");
        return;
	}
    const char *graphics=getenv("NAVIT_GRAPHICS");
	if (!graphics)
		graphics="gtk_drawing_area";
	struct attr graphics_type={attr_type};graphics_type.u.str=(char *)graphics;
    struct attr graphics_w={attr_w};graphics_w.u.num=MapViewSize.getHorizontalSize();
    struct attr graphics_h={attr_h};graphics_h.u.num=MapViewSize.getVerticalSize();
	struct attr graphics_window_title={attr_window_title};graphics_window_title.u.str=g_strdup_printf("Navit-%d",handle);
	struct attr *graphics_attrs[]={&graphics_type,&graphics_w,&graphics_h,&graphics_window_title,NULL};
	m_graphics.type=attr_graphics;
	m_graphics.u.graphics=graphics_new(&m_navit,graphics_attrs);
    g_free(graphics_window_title.u.str);

	if (!m_graphics.u.graphics) {
        dbg(lvl_debug,"failed to create new graphics\n");
		return;
	}
    m_postdraw_callback=callback_new_attr_1(reinterpret_cast<void (*)(void)>(MapViewerControlObj_PostDraw), attr_postdraw, this);
	m_move_callback=callback_new_1(reinterpret_cast<void (*)(void)>(MapViewerControlObj_MoveMap), this);
	graphics_add_callback(m_graphics.u.graphics, m_postdraw_callback);
	navit_add_attr(m_navit.u.navit, &m_graphics);

	struct attr mapset;
	if (!navit_get_attr(navit_template.u.navit, attr_mapset, &mapset, NULL)) {
        dbg(lvl_debug,"failed to get mapset\n");
		return;
	}
    mapset.u.mapset=mapset_dup(mapset.u.mapset);
	m_mapset=mapset.u.mapset;
	navit_add_attr(m_navit.u.navit, &mapset);

	struct attr_iter *iter=navit_attr_iter_new();
	struct attr layout;
	while (navit_get_attr(navit_template.u.navit, attr_layout, &layout, iter)) {
		navit_add_attr(m_navit.u.navit, &layout);
	}
	navit_attr_iter_destroy(iter);

	struct attr vehicle_source={attr_source};vehicle_source.u.str=(char *)"null:";
	struct attr vehicle_follow={attr_follow};vehicle_follow.u.num=1;
	struct attr *vehicle_attrs[]={&vehicle_source,&vehicle_follow,NULL};
	m_vehicle.type=attr_vehicle;
	m_vehicle.u.vehicle=vehicle_new(&m_navit,vehicle_attrs);
	navit_add_attr(m_navit.u.navit, &m_vehicle);
	navit_set_attr(m_navit.u.navit, &m_vehicle);
    //connect the map matched position client to map viewer
    mp_mapMatchedPositionClientProxy->connectToMapViewer(this);
    // and connect it to the vehicle
    mp_mapMatchedPositionClientProxy->connectToVehicle(m_vehicle.u.vehicle);
	navit_init(m_navit.u.navit);
	graphics_get_data(m_graphics.u.graphics,"window");
	struct transformation *trans=navit_get_trans(m_navit.u.navit);
	struct map_selection sel;
	memset(&sel, 0, sizeof(sel));
    sel.u.p_rect.rl.x=MapViewSize.getHorizontalSize();
    sel.u.p_rect.rl.y=MapViewSize.getVerticalSize();
	transform_set_screen_selection(trans, &sel);

#if LM
    t_ilm_nativedisplay display = (t_ilm_nativedisplay)graphics_get_data(m_graphics.u.graphics, "display");

    if (ilmClient_init(display) != ILM_SUCCESS) {
        fprintf(stderr, "error on ilm_initWidthNativeDisplay\n");
    }

    t_ilm_nativehandle nativehandle=(t_ilm_nativehandle)graphics_get_data(m_graphics.u.graphics,"xwindow_id");
    t_ilm_surface surfaceId=FSA_SURFACE;

    //Register Notification
    if (ilm_registerNotification(callbackFunction, NULL)  != ILM_SUCCESS) {
        fprintf(stderr,"error on ilm_registerNotification\n");
    }

    //Create surface for the map and add notification when created. Note: width, heigh & pixel format are not taking in count at the end.
    if (ilm_surfaceCreate(nativehandle, MapViewSize._1, MapViewSize._2, ILM_PIXELFORMAT_RGBA_8888, &surfaceId) != ILM_SUCCESS) {
        fprintf(stderr,"error on ilm_surfaceCreate\n");
    }

    //Commit all changes
    if (ilm_commitChanges() != ILM_SUCCESS) {
        fprintf(stderr,"error on ilm_commitChanges\n");
    }
#endif

}

MapViewerControlObj::~MapViewerControlObj()
{
#if LM
    t_ilm_surface surfaceId=FSA_SURFACE;

    if (ilm_surfaceRemove(surfaceId) != ILM_SUCCESS) {
        fprintf(stderr,"error on ilm_surfaceRemove\n");
    }

    if (ilm_commitChanges() != ILM_SUCCESS) {
        fprintf(stderr,"error on ilm_commitChanges\n");
    }
#endif

	graphics_remove_callback(m_graphics.u.graphics, m_postdraw_callback);
#if 0
	graphics_free(m_graphics.u.graphics);
	vehicle_destroy(m_vehicle.u.vehicle);
#endif
	navit_destroy(m_navit.u.navit);
    delete(mp_routingClientProxy);
    delete(mp_mapMatchedPositionClientProxy);
}

uint32_t
DisplayedRoute::RouteHandle()
{
	return m_handle;
}

void
DisplayedRoute::Show()
{
	if (!m_shown) {
		if (m_map.u.map)
			mapset_add_attr(m_mapset, &m_map);
		m_shown=true;
	}
}

void
DisplayedRoute::Hide()
{
	if (m_shown) {
		if (m_map.u.map)
			mapset_remove_attr(m_mapset, &m_map);
		m_shown=false;
	}
}

void
DisplayedRoute::AddGeoCoordinateD(double lat, double lon)
{
	struct coord_geo g;
	struct coord c;
	g.lat=lat;
	g.lng=lon;
	transform_from_geo(projection_mg, &g, &c);
	m_coordinates.push_back(c);
}

bool
DisplayedRoute::AddSegment(Routing::RouteSegment map)
{
    if (map.find(Routing::RouteSegmentType::START_LATITUDE) != map.end() && map.find(Routing::RouteSegmentType::START_LONGITUDE) != map.end())
        AddGeoCoordinateD(map[Routing::RouteSegmentType::START_LATITUDE].get<double>(),map[Routing::RouteSegmentType::START_LONGITUDE].get<double>());
    if (map.find(Routing::RouteSegmentType::INTERMEDIATE_POINTS) != map.end()) {
        std::vector<Routing::IntermediatePoint> intermediate_points=map[Routing::RouteSegmentType::INTERMEDIATE_POINTS].get<std::vector<Routing::IntermediatePoint>>();
        Routing::IntermediatePoint intermediate_point;
        for (size_t i = 0 ; i < intermediate_points.size(); i++) {
            intermediate_point = intermediate_points[i];
            AddGeoCoordinateD(intermediate_point.getLatitude(), intermediate_point.getLongitude());
		}
	}
    if (map.find(Routing::RouteSegmentType::END_LATITUDE) != map.end() && map.find(Routing::RouteSegmentType::END_LONGITUDE) != map.end()) {
        AddGeoCoordinateD(map[Routing::RouteSegmentType::END_LATITUDE].get<double>(),map[Routing::RouteSegmentType::END_LONGITUDE].get<double>());
		return true;
	}
	return false;
}

void
DisplayedRoute::WriteSegment(FILE *out)
{
	if (m_coordinates.size()) {
        size_t i;
		uint32_t header[3]={2+2*m_coordinates.size(),type_street_route,2*m_coordinates.size()};
		fwrite(header, sizeof(header), 1, out);
		for (i = 0 ; i < m_coordinates.size() ; i++) 
			fwrite(&m_coordinates[i], sizeof(struct coord), 1, out);
	}
}

DisplayedRoute::DisplayedRoute(class MapViewerControlObj *mapviewer, uint8_t RouteSession, NavigationTypes::Handle RouteHandle, struct mapset *mapset)
{	
    dbg(lvl_debug,"enter\n");
    std::vector<Routing::RouteSegment> RouteShape;
    std::vector<Routing::RouteSegmentType> valuesToReturn;
    valuesToReturn.push_back(Routing::RouteSegmentType::START_LATITUDE);
    valuesToReturn.push_back(Routing::RouteSegmentType::START_LONGITUDE);
    valuesToReturn.push_back(Routing::RouteSegmentType::END_LATITUDE);
    valuesToReturn.push_back(Routing::RouteSegmentType::END_LONGITUDE);
    valuesToReturn.push_back(Routing::RouteSegmentType::INTERMEDIATE_POINTS);
	m_handle=RouteHandle;
	m_map.type=attr_map;
	m_map.u.map=NULL;
	m_filename=NULL;
	m_mapset=mapset;
	m_shown=false;
	uint32_t totalNumberOfSegments;
    CommonAPI::CallStatus status;
    mp_routingClientProxy->myServiceRouting->getRouteSegments(RouteHandle, 1, valuesToReturn, 0xffffffff, 0, status,totalNumberOfSegments, RouteShape);
    m_filename=g_strdup_printf("/tmp/genivi_route_map_%d_%d.bin",mapviewer->m_handle,RouteHandle);
	FILE *f=fopen(m_filename,"w");
	int count=RouteShape.size();
	bool complete=true;
	for (int i = 0 ; i < count ; i++) {
        Routing::RouteSegment map = RouteShape[i];
		if (!complete) {
            if (map.find(Routing::RouteSegmentType::START_LATITUDE) != map.end() && map.find(Routing::RouteSegmentType::START_LONGITUDE) != map.end())
                AddGeoCoordinateD(map[Routing::RouteSegmentType::START_LATITUDE].get<double>(),map[Routing::RouteSegmentType::START_LONGITUDE].get<double>());
			else
                dbg(lvl_debug,"previous segment is missing end, but current segment is missing start also");
			WriteSegment(f);
		}
		complete=AddSegment(map);
	}
	if (!complete)
        dbg(lvl_debug,"last segment is missing end");
	WriteSegment(f);
	fclose(f);
	struct attr map_attr_type={attr_type};
	map_attr_type.u.str=(char *)"binfile";
	struct attr map_attr_data={attr_data};
	map_attr_data.u.str=m_filename;
	struct attr *map_attrs[]={&map_attr_type, &map_attr_data, NULL};
	m_map.u.map=map_new(NULL,map_attrs);
}

DisplayedRoute::~DisplayedRoute()
{
    dbg(lvl_debug,"enter\n");
	if (m_map.u.map)
		map_destroy(m_map.u.map);
	if (m_filename) {
		unlink(m_filename);
		g_free(m_filename);
	}
}

void
plugin_init(void)
{
    DLT_REGISTER_APP("MPVS","MAP VIEWER CONTROL SERVER");
    DLT_REGISTER_CONTEXT(gCtx,"MPVS","Global Context");

    dbg(lvl_debug,"enter\n");
    event_request_system("glib","genivi_mapviewercontrol");

    // Common API data init
    runtime = CommonAPI::Runtime::get();

    // init the map viewer control server
    const std::string domain = "local";
    const std::string instanceMapViewerControlServer = "MapViewerControl";

    std::shared_ptr<MapViewerControlServerStub> myServiceMapViewerControl = std::make_shared<MapViewerControlServerStub>();

    bool successfullyRegistered = runtime->registerService(domain, instanceMapViewerControlServer, myServiceMapViewerControl);
    while (!successfullyRegistered) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        successfullyRegistered = runtime->registerService(domain, instanceMapViewerControlServer, myServiceMapViewerControl);
    }

    LOG_INFO_MSG(gCtx,"map viewer control server");

    //init the session client
    const std::string instanceNavigationCoreSessionClient = "Session";
    mp_navigationCoreSessionClientProxy = new NavigationCoreSessionClientProxy(domain,instanceNavigationCoreSessionClient);

    LOG_INFO_MSG(gCtx,"session client");

    //init the routing client
    const std::string instanceRoutingClient = "Routing";
    mp_routingClientProxy = new RoutingClientProxy(domain,instanceRoutingClient);
    mp_routingClientProxy->setListeners();

    LOG_INFO_MSG(gCtx,"routing client");

    // init the map matched position client
    const std::string instanceMapMatchedPositionClient = "MapMatchedPosition";
    mp_mapMatchedPositionClientProxy = new MapMatchedPositionClientProxy(domain,instanceMapMatchedPositionClient);
    mp_mapMatchedPositionClientProxy->setListeners();

    LOG_INFO_MSG(gCtx,"map matched position client");

#if LM
    if (ilm_init() != ILM_SUCCESS) {
        fprintf(stderr,"error on ilm_init\n");
    }
#endif

}