如何在Google地球中创建kml到地理编码地址?

时间:2013-03-20 13:33:43

标签: excel-vba geocoding kml google-earth vba

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

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

我知道某些Google Maps 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

1 个答案:

答案 0 :(得分:2)

使用&lt; address&gt; 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>