excel - 如何创建 kml 来对 Google 地球中的地址进行地理编码?

标签 excel geocoding kml google-earth vba

如果您有一个包含地址信息的 .csv 文件,您可以将其拖放到 Google 地球(以下称为 GE)上,然后会出现一个向导,以便您分配字段,然后 GE 将开始地理编码位置并为每个地址创建点(纬度和经度)。然后,您可以将地理编码文件导出为 kml 并获取每个点的纬度/经度。然而,此过程有 2500 行的限制。

我听说可以创建一个 kml(或者可能是一个 xml)文件并将其拉入 GE 中以启动地理编码过程,并且此方法没有行限制。有人可以帮我弄清楚如何做到这一点吗?我尝试了以下代码,它成功导入到 GE 但没有创建任何点。

我知道一些 Google map API 可以执行类似的操作,并且对于商业用途每天的行数限制为 100,000 行,但我真的很希望与 GE 合作使用该流程。

<?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>GEImport1.csv</name>
    <Schema name="GEImport1" id="S_GEImport1_S">
        <SimpleField type="string" name="Adress"><displayName>&lt;b&gt;Adress&lt;/b&gt;</displayName>
</SimpleField>
    </Schema>
    <Style id="hlightPointStyle">
        <IconStyle>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle_highlight.png</href>
            </Icon>
        </IconStyle>
        <BalloonStyle>
            <text><![CDATA[<table border="0">
  <tr><td><b>Adress</b></td><td>$[GEImport1/Adress]</td></tr>
</table>
]]></text>
        </BalloonStyle>
    </Style>
    <Style id="normPointStyle">
        <IconStyle>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href>
            </Icon>
        </IconStyle>
        <BalloonStyle>
            <text><![CDATA[<table border="0">
  <tr><td><b>Adress</b></td><td>$[GEImport1/Adress]</td></tr>
</table>
]]></text>
        </BalloonStyle>
    </Style>
    <StyleMap id="pointStyleMap">
        <Pair>
            <key>normal</key>
            <styleUrl>#normPointStyle</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#hlightPointStyle</styleUrl>
        </Pair>
    </StyleMap>
    <Folder id="layer 0">
        <name>GEImport1</name>
        <visibility>0</visibility>
        <Placemark>
            <visibility>0</visibility>
            <styleUrl>#pointStyleMap</styleUrl>
            <ExtendedData>
                <SchemaData schemaUrl="#S_GEImport1_S">
                    <SimpleData name="Adress">6212 GATUN CT Port ST Lucie, FL</SimpleData>
                </SchemaData>
            </ExtendedData>
        </Placemark>
        <Placemark>
            <visibility>0</visibility>
            <styleUrl>#pointStyleMap</styleUrl>
            <ExtendedData>
                <SchemaData schemaUrl="#S_GEImport1_S">
                    <SimpleData name="Adress">6213 DIANA CT Port ST Lucie, FL</SimpleData>
                </SchemaData>
            </ExtendedData>
        </Placemark>
        <Placemark>
            <visibility>0</visibility>
            <styleUrl>#pointStyleMap</styleUrl>
            <ExtendedData>
                <SchemaData schemaUrl="#S_GEImport1_S">
                    <SimpleData name="Adress">6213 DUKE CIR Port ST Lucie, FL</SimpleData>
                </SchemaData>
            </ExtendedData>
        </Placemark>
    </Folder>
</Document>
</kml>

编辑:最终结果
JasonM1 的解决方案非常有效! 我最终得到了以下格式的 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>AddressImport</name>
    <Style id="normPointStyle">
        <IconStyle>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href>
            </Icon>
        </IconStyle>
        <BalloonStyle>
            <text><![CDATA[<table border="0">
          <tr><td><b>Address</b></td><td>$[address]</td></tr>
          </table>]]>
      </text>
        </BalloonStyle>
    </Style>
    <StyleMap id="pointStyleMap">
        <Pair>
            <key>normal</key>
            <styleUrl>#normPointStyle</styleUrl>
        </Pair>
    </StyleMap>
    <Folder id="layer 0">
        <name>AddressImport</name>
        <visibility>1</visibility>
        <Placemark>
            <visibility>1</visibility>
            <address>9994 CHADWICK DR Port ST Lucie, FL</address>
            <styleUrl>#pointStyleMap</styleUrl>
        </Placemark>
        <Placemark>
            <visibility>1</visibility>
            <address>9995 AMBROSE WAY Port ST Lucie, FL</address>
            <styleUrl>#pointStyleMap</styleUrl>
        </Placemark>
        <Placemark>
            <visibility>1</visibility>
            <address>9997 STONEGATE DR Port ST Lucie, FL</address>
            <styleUrl>#pointStyleMap</styleUrl>
        </Placemark>
    </Folder>
</Document>
</kml>

此外,我使用以下 Excel VBA 代码自动创建 kml 文件:

Sub CreateCSV_FSO()
    Dim objFSO
    Dim objTF
    Dim ws As Worksheet
    Dim lRow As Long
    Dim lCol As Long
    Dim strTmp As String
    Dim lFnum As Long
    Dim sFilePath As String
    Dim i As Long
    Dim x As Integer 'used to represent the column that contains the address
    sFilePath = ActiveWorkbook.Path & "\AddressImport.kml"

    Set objFSO = CreateObject("scripting.filesystemobject")
    Set objTF = objFSO.createtextfile(sFilePath, True, False)

    'Header information
    objTF.writeline "<?xml version=""1.0"" encoding=""UTF-8""?>"
    objTF.writeline "<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"">"
    objTF.writeline "    <Document>"
    objTF.writeline "<name>AddressImport</name>"
    objTF.writeline "    <Style id=""normPointStyle"">"
    objTF.writeline "        <IconStyle>"
    objTF.writeline "            <Icon>"
    objTF.writeline "                <href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href>"
    objTF.writeline "            </Icon>"
    objTF.writeline "        </IconStyle>"
    objTF.writeline "        <BalloonStyle>"
    objTF.writeline "            <text><![CDATA[<table border=""0"">"
    objTF.writeline "          <tr><td><b>Address</b></td><td>$[address]</td></tr>"
    objTF.writeline "          </table>]]>"
    objTF.writeline "      </text>"
    objTF.writeline "        </BalloonStyle>"
    objTF.writeline "    </Style>"
    objTF.writeline "    <StyleMap id=""pointStyleMap"">"
    objTF.writeline "        <Pair>"
    objTF.writeline "            <key>normal</key>"
    objTF.writeline "            <styleUrl>#normPointStyle</styleUrl>"
    objTF.writeline "        </Pair>"
    objTF.writeline "    </StyleMap>"
    objTF.writeline "    <Folder id=""layer 0"">"
    objTF.writeline "        <name>AddressImport</name>"
    objTF.writeline "        <visibility>1</visibility>"

    'input the number representative of the column that contains the address
    x = 7 'a=1,b=2,c=3,d=4.....etc.
    i = 2
    While Cells(i, x) <> ""

        strTmp = ""
        strTmp = Cells(i, x)
        strTmp = Replace(strTmp, "&", "and")

        objTF.writeline "        <Placemark>"
        objTF.writeline "            <visibility>1</visibility>"
        objTF.writeline "            <address>" & strTmp & "</address>"
        objTF.writeline "            <styleUrl>#pointStyleMap</styleUrl>"
        objTF.writeline "        </Placemark>"


        strTmp = Cells(i, x)
        i = i + 1
    Wend

    objTF.writeline "    </Folder>"
    objTF.writeline "</Document>"
    objTF.writeline "</kml>"


    objTF.Close
    Set objFSO = Nothing
    MsgBox "Done!", vbOKOnly

End Sub

最佳答案

使用< address > KML 中的元素,Google 地球将在其中自动为您对地址进行地理编码。

<Placemark>
    <visibility>0</visibility>
    <address>6213 DUKE CIR Port ST Lucie, FL</address>
    <styleUrl>#pointStyleMap</styleUrl>             
</Placemark>

您可以使用<address>标签来指定点的位置,而不是使用纬度和经度坐标。 (但是,如果提供 <Point> ,则它优先于 <address> 。)

此外,您不再需要 Schema 和 Adress SchemaData 元素,而是可以直接内联 BalloonStyle 文本中的 $[address] 字段:

<BalloonStyle>
      <text><![CDATA[<table border="0">
          <tr><td><b>Adress</b></td><td>$[address]</td></tr>
          </table>]]>
      </text>
</BalloonStyle>

在 Google 地球中加载 KML 文件并保存后,您会注意到 Google 地球会将其更改为以下内容并填充坐标:

<Placemark>
    <address>6213 DUKE CIR Port ST Lucie, FL</address>
    <styleUrl>#pointStyleMap</styleUrl>
    <MultiGeometry>
        <Point>
            <coordinates>-80.36086,27.366379,0</coordinates>
        </Point>
        <LinearRing>
            <coordinates>
                -80.36086,27.366379,0 -80.36086,27.366379,0 -80.36086,27.366379,0 -80.36086,27.366379,0 -80.36086,27.366379,0 
            </coordinates>
        </LinearRing>
    </MultiGeometry>
</Placemark>

关于excel - 如何创建 kml 来对 Google 地球中的地址进行地理编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15525071/

相关文章:

excel - 如何使VBA函数 "VBA only"并将其作为UDF禁用

vba - Excel VBA : Simplify long Code

google-maps-api-3 - google-maps kml,如何标记线串?

javascript - 如何在网络链接刷新时将 Google 地球插件的时间 slider 设置为当前时间?

python-2.7 - 将 GeoDataFrame 多边形转换为 kml 文件

excel - 以编程方式获取工作表允许的最大列数和行数

Excel VBA按钮单元格地址错误

google-maps - 使用 google map 或 openstreetmaps API 绘制大城市的区域

php - 使用多边形或圆形作为搜索查找纬度/经度

python - 如何在python中从给定的纬度和经度找到500米以内的用户位置