愿你出走半生,归来仍是少年!
上一篇文章构建出来基础的白球,现在需要给它添加底图啦。先上最常用的天地图。
1.天地图
天地图做过Gis开发的应该都知道,需要先申请key然后才能使用。然后天地图是基于XYZ的标准进行切片的,所以直接使用OSGEarth中的XYZImageLayer进行加载。
2.代码
此处封装了一个静态函数,方便后期使用。
/// <summary>
/// 创建天地图图层
/// </summary>
/// <param name="key">天地图key</param>
/// <param name="isVector">是否是矢量图层</param>
/// <returns>天地图瓦片图层</returns>
osgEarth::XYZImageLayer* Cv::LayerFactory::CreateTdtTileLayer(std::string key, bool isVector)
{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");std::string lySourceName = isVector ? "vec_w" : "img_w";std::string lyName = isVector ? "天地图矢量" : "天地图影像";osgEarth::URI imgUri("http://t[01234567].tianditu.com/DataServer?T="+ lySourceName +"&tk="+ key +"&l={z}&x={x}&y={y}",context);osgEarth::XYZImageLayer* imgLy = new osgEarth::XYZImageLayer();imgLy->setURL(imgUri);imgLy->setProfile(osgEarth::Profile::create("spherical-mercator"));imgLy->setName(lyName);imgLy->setOpacity(1);return imgLy;
}/// <summary>
/// 创建天地图注记瓦片图层
/// </summary>
/// <param name="key">天地图key</param>
/// <param name="isVector">是否是矢量图层</param>
/// <returns></returns>
osgEarth::XYZImageLayer* Cv::LayerFactory::CreateTdtRemarkTileLayer(std::string key, bool isVector)
{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");std::string lySourceName = isVector ? "cva_w" : "cia_w";std::string lyName = isVector ? "天地图矢量注记" : "天地图影像注记";osgEarth::URI imgUri("http://t[01234567].tianditu.com/DataServer?T=" + lySourceName + "&tk=" + key + "&l={z}&x={x}&y={y}",context);osgEarth::XYZImageLayer* imgLy = new osgEarth::XYZImageLayer();imgLy->setURL(imgUri);imgLy->setName(lyName);imgLy->setOpacity(1);imgLy->setProfile(osgEarth::Profile::create("spherical-mercator"));return imgLy;
}
3.效果
通过osgEarth::Map的addLayer添加图层即可完成天地图的加载。
4.参考
osgearth 加载天地图(亲测可行)_osgearth 天地图_祝太勇的博客-CSDN博客