osgEarth::URIContext context;
context.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/"
"*;q=0.8,application/signed-exchange;v=b3;q=0.9");
context.addHeader("Accept-Encoding", "gzip, deflate");
context.addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
context.addHeader("Cache-Control", "max-age=0");
context.addHeader("Connection", "keep-alive");
context.addHeader("Upgrade-Insecure-Requests", "1");
context.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.39");
osgEarth::URI uri("https://t0.tianditu.gov.cn/img_w/"
"wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&"
"FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=767749dc29e1e159c2ef02a5687aa5ce",
context);
osgEarth::XYZImageLayer* TianDiTu = new osgEarth::XYZImageLayer();
TianDiTu->setURL(uri);
TianDiTu->setProfile(Profile::create("spherical-mercator"));
auto pmap = app._mapNode->getMap();
pmap->addLayer(TianDiTu);
上面的字符串中有个 tk=767749dc29e1e159c2ef02a5687aa5ce
等于号后面的token是需要去天地图官网申请的;申请好了,替换一下就可以了。
效果显示如下: