kml" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

C# 程序自動批量生成 google maps 的KML文件

系統 3012 0

google maps 的 KML 文件可以用于靜態的地圖標注,在某些應用中,我們手上往往有成百上千個地址,我們需要把這些地址和描述批量標注到 google maps 上去,如果手工來做,太耗時間,在這里我寫了一個程序批量來生成這個 KML 文件。

首先看一下 KML 文件的格式:

    
      <?
    
    
      xml
    
    
      version
    
    
      ="1.0"
    
    
      encoding
    
    
      ="UTF-8"
    
    ?
    
      >
    
    
      <
    
    
      kml
    
    
      xmlns
    
    
      ="http://www.google.com/earth/kml/2"
    
    
      >
    
    
      <
    
    
      Document
    
    
      >
    
    
      <
    
    
      name
    
    
      >
    
    kml_sample2.kml
    
      </
    
    
      name
    
    
      >
    
    
      <
    
    
      Style
    
    
      id
    
    
      ="red"
    
    
      >
    
    
      <
    
    
      IconStyle
    
    
      >
    
    
      <
    
    
      Icon
    
    
      >
    
    
      <
    
    
      href
    
    
      >
    
    http://www.google.com/intl/en_us/mapfiles/ms/icons/red-dot.png
    
      </
    
    
      href
    
    
      >
    
    
      </
    
    
      Icon
    
    
      >
    
    
      </
    
    
      IconStyle
    
    
      >
    
    
      </
    
    
      Style
    
    
      >
    
    
      <
    
    
      Style
    
    
      id
    
    
      ="green"
    
    
      >
    
    
      <
    
    
      IconStyle
    
    
      >
    
    
      <
    
    
      Icon
    
    
      >
    
    
      <
    
    
      href
    
    
      >
    
    http://www.google.com/intl/en_us/mapfiles/ms/icons/green-dot.png
    
      </
    
    
      href
    
    
      >
    
    
      </
    
    
      Icon
    
    
      >
    
    
      </
    
    
      IconStyle
    
    
      >
    
    
      </
    
    
      Style
    
    
      >
    
    
      <
    
    
      Style
    
    
      id
    
    
      ="blue"
    
    
      >
    
    
      <
    
    
      IconStyle
    
    
      >
    
    
      <
    
    
      Icon
    
    
      >
    
    
      <
    
    
      href
    
    
      >
    
    http://www.google.com/intl/en_us/mapfiles/ms/icons/blue-dot.png
    
      </
    
    
      href
    
    
      >
    
    
      </
    
    
      Icon
    
    
      >
    
    
      </
    
    
      IconStyle
    
    
      >
    
    
      </
    
    
      Style
    
    
      >
    
    
      <
    
    
      Placemark
    
    
      >
    
    
      <
    
    
      name
    
    
      >
    
    Google Inc.
    
      </
    
    
      name
    
    
      >
    
    
      <
    
    
      description
    
    
      ><!
    
    [CDATA[
      Google Inc.
    
      <
    
    
      br
    
    
      />
    
    
      1600 Amphitheatre Parkway
    
      <
    
    
      br
    
    
      />
    
    
      Mountain View, CA 94043
    
      <
    
    
      br
    
    
      />
    
    
      Phone: +1 650-253-0000
    
      <
    
    
      br
    
    
      />
    
    
      Fax: +1 650-253-0001
    
      <
    
    
      br
    
    
      />
    
    
      <
    
    
      p
    
    
      >
    
    Home page: 
    
      <
    
    
      a
    
    
      href
    
    
      ="http://www.google.com"
    
    
      >
    
    www.google.com
    
      </
    
    
      a
    
    
      ></
    
    
      p
    
    
      >
    
    
    ]]
    
      >
    
    
      </
    
    
      description
    
    
      >
    
    
      <
    
    
      styleUrl
    
    
      >
    
    #red
    
      </
    
    
      styleUrl
    
    
      >
    
    
      <
    
    
      Point
    
    
      >
    
    
      <
    
    
      coordinates
    
    
      >
    
    -122.0841430, 37.4219720, 0
    
      </
    
    
      coordinates
    
    
      >
    
    
      </
    
    
      Point
    
    
      >
    
    
      </
    
    
      Placemark
    
    
      >
    
    
      <
    
    
      Placemark
    
    
      >
    
    
      <
    
    
      name
    
    
      >
    
    Yahoo! Inc.
    
      </
    
    
      name
    
    
      >
    
    
      <
    
    
      description
    
    
      ><!
    
    [CDATA[
      Yahoo! Inc.
    
      <
    
    
      br
    
    
      />
    
    
      701 First Avenue
    
      <
    
    
      br
    
    
      />
    
    
      Sunnyvale, CA 94089
    
      <
    
    
      br
    
    
      />
    
    
      Tel: (408) 349-3300
    
      <
    
    
      br
    
    
      />
    
    
      Fax: (408) 349-3301
    
      <
    
    
      br
    
    
      />
    
    
      <
    
    
      p
    
    
      >
    
    Home page: 
    
      <
    
    
      a
    
    
      href
    
    
      ="http://yahoo.com"
    
    
      >
    
    http://yahoo.com
    
      </
    
    
      a
    
    
      ></
    
    
      p
    
    
      >
    
    
      ]]
    
      >
    
    
      </
    
    
      description
    
    
      >
    
    
      <
    
    
      styleUrl
    
    
      >
    
    #green
    
      </
    
    
      styleUrl
    
    
      >
    
    
      <
    
    
      Point
    
    
      >
    
    
      <
    
    
      coordinates
    
    
      >
    
    -122.0250403,37.4163228
    
      </
    
    
      coordinates
    
    
      >
    
    
      </
    
    
      Point
    
    
      >
    
    
      </
    
    
      Placemark
    
    
      >
    
    
      </
    
    
      Document
    
    
      >
    
    
      </
    
    
      kml
    
    
      >
    
  

這個是一個典型的用于google maps 的 KML 文件,(注意不同應用的KML 格式會有所不同,比如 google earth 的 kml 格式就復雜得多)

從這個kml 文件格式來看,其實它就是一個 xml 文件,我們只要自動生成這個文件中各個元素的信息就可以得到這個xml? 文件。這里其實最大的問題是如何自動通過地址獲取經緯度坐標。值得慶幸的是 google 提供了這方面的 api 函數。google api 獲取地理坐標的官方例子見: geocodingapi

我的實現稍微復雜一些,因為我需要在函數中為不同的位置自動分配顏色

?

      
           1:  
      
      
        /// <summary>
      
    
      
           2:  
      
      
        /// Generate placemark by address description
      
    
      
           3:  
      
      
        /// </summary>
      
    
      
           4:  
      
      
        /// <param name="addrDescription">address and description</param>
      
    
      
           5:  
      
      
        /// <returns>if no matched, return false</returns>
      
    
      
           6:  
      
      
        public
      
      
        bool
      
       Generate(AddressDescription addrDescription)
    
      
           7:  
      
              {
    
      
           8:  
      
                  _LastErrorOrWarning = 
      
        null
      
      ;
    
      
           9:  
      
      ?
    
      
          10:  
      
                  Thread.Sleep(DelayInMs);
    
      
          11:  
      
      ?
    
      
          12:  
      
                  List<GeographicCoordinate> coordinates = Geocoding.Geocode(addrDescription.Address);
    
      
          13:  
      
      ?
    
      
          14:  
      
      
        if
      
       (coordinates.Count == 0)
    
      
          15:  
      
                  {
    
      
          16:  
      
                      _LastErrorOrWarning = 
      
        string
      
      .Format(
      
        "Address:{0}, Description:{1} does not find the coordinates, please make sure the address is correctly."
      
      ,
    
      
          17:  
      
                          addrDescription.Address, addrDescription.Description);
    
      
          18:  
      
      ?
    
      
          19:  
      
      
        return
      
      
        false
      
      ;
    
      
          20:  
      
                  }
    
      
          21:  
      
      ?
    
      
          22:  
      
      
        if
      
       (coordinates.Count > 1)
    
      
          23:  
      
                  {
    
      
          24:  
      
                      _LastErrorOrWarning = 
      
        string
      
      .Format(
      
        "Address:{0}, Description:{1} has more than one coordinates."
      
      ,
    
      
          25:  
      
                          addrDescription.Address, addrDescription.Description);
    
      
          26:  
      
                  }
    
      
          27:  
      
      ?
    
      
          28:  
      
      
        string
      
       colorId = Colors[_ColorIndex];
    
      
          29:  
      
      ?
    
      
          30:  
      
                  _ColorIndex++;
    
      
          31:  
      
      ?
    
      
          32:  
      
      
        if
      
       (_ColorIndex >= Colors.Count)
    
      
          33:  
      
                  {
    
      
          34:  
      
                      _ColorIndex = 0;
    
      
          35:  
      
                  }
    
      
          36:  
      
      ?
    
      
          37:  
      
                  _Kml.Document.Add(
      
        new
      
       Placemark(addrDescription.Address, addrDescription.Description, colorId,
    
      
          38:  
      
                      coordinates[0].Latitude, coordinates[0].Longitude));
    
      
          39:  
      
      ?
    
      
          40:  
      
      
        return
      
      
        true
      
      ;
    
      
          41:  
      
              }
    

?

第32行有個bug,應該是 >=? ,我原來寫成 > 了,博客中我改過來了,源碼我就不改了。

如上代碼,第12行就是通過GeocodingApi 獲取指定地址的物理坐標,由于有時候獲取不到坐標,有時候由于地址不確切,有多個坐標,所以我加了一個錯誤和警告的屬性,用于調用者得到相關的信息。

_Kml 這個對象是一個 Kml 類的實例,這個類用于生成 KML 文件結構,并可以保存到KML文件中。這個類在后面介紹。

下面的 _Color 部分是自動的順序分配標注點的顏色,我為了省事,在代碼中寫死了4種顏色,你也可以修改代碼增加顏色或其他圖標。

標注顏色這里其實還有一個問題,就是如果讓相鄰的節點顯示不同顏色,這個算法比較復雜了,我沒有實現,各位如果有興趣可以思考一下這個怎么做。

好了,最大的問題解決了,剩下就是寫 xml? 文件了,這個很簡單,我就不深入講了,直接把代碼貼出來。

    
      using
    
     System;

    
      using
    
     System.Collections.Generic;

    
      using
    
     System.Linq;

    
      using
    
     System.Text;

    
      using
    
     System.Xml;

    
      using
    
     System.Xml.Serialization;

    
      using
    
     System.IO;


    
      namespace
    
     GenerateKML
{
    
    
      public
    
    
      class
    
     Placemark
    {
        
    
      public
    
    
      class
    
     KMLPoint
        {
            
    
      public
    
     KMLPoint()
            {
            }

            
    
      public
    
     KMLPoint(
    
      double
    
     latitude, 
    
      double
    
     longitude)
            {
                SetCoordinates(latitude, longitude);
            }

            
    
      private
    
    
      string
    
     _coordinates;

            
    
      public
    
    
      void
    
     SetCoordinates(
    
      double
    
     latitude, 
    
      double
    
     longitude)
            {
                _coordinates = longitude.ToString() + 
    
      ","
    
     + latitude.ToString();
            }

            
    
      public
    
    
      string
    
     coordinates
            {
                get
                {
                    
    
      return
    
     _coordinates;
                }

                set
                {
                    _coordinates = 
    
      value
    
    ;
                }
            }
        }

        [XmlElement(
    
      "name"
    
    )]
        
    
      public
    
    
      string
    
     Name { get; set; }

        [XmlElement(
    
      "description"
    
    )]
        
    
      public
    
    
      string
    
     Description { get; set; }

        [XmlElement(
    
      "styleUrl"
    
    )]
        
    
      public
    
    
      string
    
     StyleUrl { get; set; }

        
    
      public
    
     KMLPoint Point { get; set; }

        
    
      public
    
     Placemark()
        {
        }

        
    
      public
    
     Placemark(
    
      string
    
     name, 
    
      string
    
     description, 
    
      string
    
     styleUrl,
            
    
      double
    
     latitude, 
    
      double
    
     longitude)
        {
            Name = name;
            Description = description;
            StyleUrl = styleUrl;

            Point = 
    
      new
    
     KMLPoint(latitude, longitude);
        }

    }

    
    
      public
    
    
      class
    
     kml
    {
        [XmlIgnore]
        
    
      string
    
     Name { get; set; }


        List<Placemark> _Placemarks = 
    
      new
    
     List<Placemark>();

        [XmlArray()]
        
    
      public
    
     List<Placemark> Document
        {
            get
            {
                
    
      return
    
     _Placemarks;
            }

            set
            {
                _Placemarks = 
    
      value
    
    ;
            }
        }

        
    
      public
    
     kml()
        {
        }

        
    
      public
    
     kml(
    
      string
    
     name)
        {
            Name = name;
        }

        
    
      private
    
     XmlNode GetColorStyle(XmlDocument xmlDoc,  
    
      string
    
     color)
        {
            XmlNode style = xmlDoc.CreateNode(XmlNodeType.Element, 
    
      "Style"
    
    , 
    
      ""
    
    );
            XmlAttribute attr = style.OwnerDocument.CreateAttribute(
    
      "id"
    
    );
            attr.Value = color;
            style.Attributes.Append(attr);

            XmlNode iconStyle = xmlDoc.CreateNode(XmlNodeType.Element, 
    
      "IconStyle"
    
    , 
    
      ""
    
    );
            XmlNode icon = xmlDoc.CreateNode(XmlNodeType.Element, 
    
      "Icon"
    
    , 
    
      ""
    
    );
            XmlNode href = xmlDoc.CreateNode(XmlNodeType.Element, 
    
      "href"
    
    , 
    
      ""
    
    );
            href.InnerText = 
    
      string
    
    .Format(
    
      "http://www.google.com/intl/en_us/mapfiles/ms/icons/{0}-dot.png"
    
    ,
                color);
            
            
            style.AppendChild(iconStyle);
            iconStyle.AppendChild(icon);
            icon.AppendChild(href);

            
    
      return
    
     style;
        }


        
    
      public
    
    
      void
    
     SaveToFile(
    
      string
    
     xml)
        {
            
    
      using
    
     (FileStream fs = 
    
      new
    
     FileStream(xml, FileMode.Create, FileAccess.ReadWrite))
            {
                
    
      using
    
     (StreamWriter sw = 
    
      new
    
     StreamWriter(fs, System.Text.Encoding.UTF8))
                {
                    XmlSerializer serializer = 
    
      new
    
     XmlSerializer(
    
      this
    
    .GetType());
                    serializer.Serialize(sw, 
    
      this
    
    );
                }
            }


            XmlDocument xmlDoc = 
    
      new
    
     XmlDocument();
            xmlDoc.Load(xml);
            xmlDoc.CreateXmlDeclaration(
    
      "1.0"
    
    , 
    
      "utf-8"
    
    , 
    
      null
    
    );

            XmlNode documentNode = xmlDoc.SelectSingleNode(
    
      @"/kml/Document"
    
    );

            XmlNode nameNode = xmlDoc.CreateNode(XmlNodeType.Element, 
    
      "name"
    
    , 
    
      ""
    
    );
            nameNode.InnerText = 
    
      this
    
    .Name;

            XmlNode placeMarkNode = documentNode.FirstChild;
            documentNode.InsertBefore(nameNode, placeMarkNode);

            documentNode.InsertBefore(GetColorStyle(xmlDoc, 
    
      "red"
    
    ), placeMarkNode);
            documentNode.InsertBefore(GetColorStyle(xmlDoc, 
    
      "green"
    
    ), placeMarkNode);
            documentNode.InsertBefore(GetColorStyle(xmlDoc, 
    
      "blue"
    
    ), placeMarkNode);
            documentNode.InsertBefore(GetColorStyle(xmlDoc, 
    
      "yellow"
    
    ), placeMarkNode);

            XmlNode kmlNode = xmlDoc.SelectSingleNode(
    
      @"/kml"
    
    );

            XmlAttribute attr = kmlNode.OwnerDocument.CreateAttribute(
    
      "xmlns"
    
    );
            attr.Value = 
    
      "http://earth.google.com/kml/2.0"
    
    ;

            kmlNode.Attributes.Append(attr);

            xmlDoc.Save(xml);
        }
    }

}

  

下面看一下調用的方法,使用者如果不想仔細研究細節,那就關注這個就可以了,調用方法非常簡單

      
           1:  
      
      
        static
      
      
        void
      
       Main(
      
        string
      
      [] args)
    
      
           2:  
      
              {
    
      
           3:  
      
                  Generator kmlGenerator = 
      
        new
      
       Generator(
      
        "Test"
      
      );
    
      
           4:  
      
      ?
    
      
           5:  
      
                  kmlGenerator.Generate(
      
        new
      
       AddressDescription(
      
        "1600 Amphitheatre Parkway, Mountain View, CA 94043"
      
      ,
    
      
           6:  
      
      
        "Google"
      
      ));
    
      
           7:  
      
      ?
    
      
           8:  
      
      
        if
      
       (!
      
        string
      
      .IsNullOrEmpty(kmlGenerator.LastErrorOrWarning))
    
      
           9:  
      
                  {
    
      
          10:  
      
                      Console.WriteLine(kmlGenerator.LastErrorOrWarning);
    
      
          11:  
      
                  }
    
      
          12:  
      
      ?
    
      
          13:  
      
                  kmlGenerator.Generate(
      
        new
      
       AddressDescription(
      
        "1 Microsoft Way, Redmond, WA 98052"
      
      ,
    
      
          14:  
      
      
        "Microsoft"
      
      ));
    
      
          15:  
      
      ?
    
      
          16:  
      
      
        if
      
       (!
      
        string
      
      .IsNullOrEmpty(kmlGenerator.LastErrorOrWarning))
    
      
          17:  
      
                  {
    
      
          18:  
      
                      Console.WriteLine(kmlGenerator.LastErrorOrWarning);
    
      
          19:  
      
                  }
    
      
          20:  
      
      ?
    
      
          21:  
      
                  kmlGenerator.Generate(
      
        new
      
       AddressDescription(
      
        "1601 S. California Ave., Palo Alto, CA 95304"
      
      ,
    
      
          22:  
      
      
        "Facebook"
      
      ));
    
      
          23:  
      
      ?
    
      
          24:  
      
      
        if
      
       (!
      
        string
      
      .IsNullOrEmpty(kmlGenerator.LastErrorOrWarning))
    
      
          25:  
      
                  {
    
      
          26:  
      
                      Console.WriteLine(kmlGenerator.LastErrorOrWarning);
    
      
          27:  
      
                  }
    
      
          28:  
      
      ?
    
      
          29:  
      
                  kmlGenerator.Generate(
      
        new
      
       AddressDescription(
      
        "701 First Ave, Sunnyvale, CA 94089"
      
      ,
    
      
          30:  
      
      
        "Yahoo"
      
      ));
    
      
          31:  
      
      ?
    
      
          32:  
      
      
        if
      
       (!
      
        string
      
      .IsNullOrEmpty(kmlGenerator.LastErrorOrWarning))
    
      
          33:  
      
                  {
    
      
          34:  
      
                      Console.WriteLine(kmlGenerator.LastErrorOrWarning);
    
      
          35:  
      
                  }
    
      
          36:  
      
      ?
    
      
          37:  
      
                  kmlGenerator.Save(
      
        "test.kml"
      
      );
    
      
          38:  
      
              }
    
      
          39:  
      
          }
    
      ?
    
      第三行,實例化 KML 生成器,并指定一個名字,這個名字對于 kml 文檔中的 name 字段。
    
      第五行,在kml 文件中標注 google 總部的地址
    
      第八行,判斷是否有最新的錯誤,每次執行第五行的Generate 方法,會將最新錯誤清空,所以這里永遠是得到最近一次調用 Generate 方法的錯誤或警告。
    
      后面以此類推了。
    
      最后 Save 到一個kml文件中就OK了。
    
      ?
    
      最后,我們可以把這個 kml 文件導入到我們自己創建的 google map 中。這個在 google maps 里面有相應的導入功能,這里就不介紹了。
    
      ?
    
      完整源碼下載
    
      ?
    
      ?
    
      注意源碼中 app.config 文件中
    
      ?
    
      
        <
      
      
        add
      
      
        key
      
      
        ="GeocodingApi.Key"
      
      
        value
      
      
        ="google api key"
      
      
        />
      
      
        <
      
      
        add
      
      
        key
      
      
        ="GeocodingApi.Url"
      
      
        value
      
      
        ="http://maps.google.com/maps/geo?"
      
      
        />
      
    

GeocodingApi.key 這里要填寫你自己的 google api key,你可以在 google? 網站上獲取,地址如下:

http://code.google.com/apis/maps/signup.html

?

      ?
    

C# 程序自動批量生成 google maps 的KML文件


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲午夜精品 | 成人久久网站 | 国产成人午夜精品5599 | 四虎高清成人永久免费影院 | 操片免费| 久青草资源福利视频 | 精品久久免费视频 | 99视频有精品 | 伊人色综合久久天天网蜜月 | 久射网| 亚洲成年人免费网站 | 久青草国产高清在线视频 | 久久久网久久久久合久久久久 | 69hdxxxx中国| 精品中文字幕一区在线 | 国产国产精品四虎视频精品 | 色偷偷91久久综合噜噜噜噜 | 99国产精品热久久久久久夜夜嗨 | 日韩高清不卡在线 | 中文字幕国产 | 四虎影院免费 | 黄片a级毛片| 日韩你懂的 | 国产乱码精品一区二区三上 | 夜夜草天天干 | 天天狠天天天天透在线 | 久热国产视频 | 美女在线国产 | 久久男人| 天天综合亚洲国产色 | 精品推荐 国产 | 视色视频| 97在线资源站 | 99热这 | 欧美一级片 在线播放 | 亚洲一二三 | 中文字幕日本在线观看 | 久久se精品动漫一区二区三区 | 神马影院伦理我不卡 | 视频一区二区三区在线观看 | 国内精品手机在线观看视频 |