Original reference: Http://docs.osgearth.org/en/latest/developer/maps.html#programmatic-map-creation
I have limited translation skills ...
Loading Earth Map File
osg::node* globe = osgdb::readnodefile ("myglobe.earth");
The simplest way
API-Programmed Map creation
The basic steps for creating a map with the API are:
1. Create a Map object
2. Add the image layer and elevation layer where you think fit
3. Create a mapnode that can draw "map objects"
4. Add the Mapnode to the scene map.
You can add layers to the map at any time:
using namespaceOsgearth;using namespaceOsgearth::D Rivers; #include<osgEarth/Map>#include<osgEarth/MapNode>#include<osgEarthDrivers/tms/TMSOptions>#include<osgEarthDrivers/gdal/GDALOptions>using namespaceOsgearth;using namespaceOsgearth::D Rivers;//Create a Map and set it to geocentric to display a Globemap* map =NewMap ();//ADD an imagery layer (blue marble from a TMS source){tmsoptions TMS; Tms.url ()="http://labs.metacarta.com/wms-c/Basic.py/1.0.0/satellite/"; Imagelayer* Layer =NewImagelayer ("NASA", TMS); Map-addimagelayer (layer);}//Add an Elevationlayer (SRTM from a local GeoTiff file){gdaloptions gdal; Gdal.url ()="c:/data/srtm.tif"; Elevationlayer* Layer =NewElevationlayer ("SRTM", Gdal); Map-addelevationlayer (layer);}//Create a mapnode to render this map:mapnode* Mapnode =NewMapnode (map); .... viewer->setscenedata (Mapnode);
(Note: The Official document is OE2.4, the current API is OE2.9, time: February 7, 2017 10:27:15)
Using Mapnode at run time
Mapnode is the node that draws the map in the scene graph
If you are not using the API to create a mapnode, you need to first use the static function get to get it:
// Load the maposg::node* Loadedmodel = Osgdb::readnodefile ("mymap.earth"); // Find the Mapnodeosgearth::mapnode* mapnode = Mapnode::get(Loadedmodel);
If you have a mapnode, you can use the map:
//Add an OpenStreetMap image sourcetmsoptions Driveropt;driveropt.url ()="http://tile.openstreetmap.org/";d Riveropt.tmstype ()="Google"; imagelayeroptions layeropt ("OSM", driveropt); Layeropt.profile ()= Profileoptions ("Global-mercator"); Imagelayer* Osmlayer =NewImagelayer (layeropt); Mapnode->getmap ()->addimagelayer (Osmlayer);
Original Translation [Osgearth] API Development Earth (OE Official document translator)