从 C++ 文件将数据输入到 KML 文件

cme*_*430 -2 c++ xml kml google-earth

我需要将坐标从 C++ 文件输入到 KML 文件中才能与 Google Earth 一起运行,您将如何执行此操作?KML 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" 
 xmlns:gx="http://www.google.com/kml/ext/2.2"
 xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom">

<Document>
<name>Path.kml</name>
<Style id="pathstyle">
    <LineStyle>
        <color>ff190cff</color>
        <width>2</width>
    </LineStyle>
</Style>

<Placemark>
    <name>Path</name>
    <description>This is the path between the 2 points</description>
    <styleUrl>#pathstyle</styleUrl>
    <LineString>
        <tessellate>1</tessellate>
        <coordinates>
            long1,lat1,0
            long2,lat2,0 
        </coordinates>
    </LineString>
</Placemark>
Run Code Online (Sandbox Code Playgroud)

当输入纬度和经度时,如何从c++文件中输入数据?它们被声明为双浮点数

Dav*_*dRR 5

这是我成功使用的一个策略:创建一系列函数来逐步构建 KML/XML。例如,下面是一个用于序列化PlacemarkKML 部分的函数:

(另请参阅现场演示。)

#include <fstream>
#include <sstream>
#include <string>

std::string FormatPlacemark(double lat1, double long1, double lat2, double long2)
{
    std::ostringstream ss;
    ss << "<Placemark>\n"
       << "<name>Path</name>\n"
       << "<description>This is the path between the 2 points</description>\n"
       << "<styleUrl>#pathstyle</styleUrl>\n"
       << "<LineString>\n"
       << "<tessellate>1</tessellate>\n"
       << "<coordinates>"
       << long1 << "," << lat1 << ",0"
       << " "
       << long2 << "," << lat2 << ",0"
       << "</coordinates>\n"
       << "</LineString>\n"
       << "</Placemark>\n";

    return ss.str();
}
Run Code Online (Sandbox Code Playgroud)

以下是创建/打开 KML 文件并写入的方法:

std::ofstream handle;

// http://www.cplusplus.com/reference/ios/ios/exceptions/
// Throw an exception on failure to open the file or on a write error.
handle.exceptions(std::ofstream::failbit | std::ofstream::badbit);

// Open the KML file for writing:
handle.open("C:/Output/Sample.kml");

// Write to the KML file:
handle << "<?xml version='1.0' encoding='utf-8'?>\n";
handle << "<kml xmlns='http://www.opengis.net/kml/2.2'>\n";

handle << FormatPlacemark(-76.2, 38.5, -76.1, 38.6);

handle << "</kml>\n";
handle.close();
Run Code Online (Sandbox Code Playgroud)