kml" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C# 程序自動(dòng)批量生成 google maps 的KML文件

系統(tǒng) 3170 0

google maps 的 KML 文件可以用于靜態(tài)的地圖標(biāo)注,在某些應(yīng)用中,我們手上往往有成百上千個(gè)地址,我們需要把這些地址和描述批量標(biāo)注到 google maps 上去,如果手工來做,太耗時(shí)間,在這里我寫了一個(gè)程序批量來生成這個(gè) 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
    
    
      >
    
  

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

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

我的實(shí)現(xiàn)稍微復(fù)雜一些,因?yàn)槲倚枰诤瘮?shù)中為不同的位置自動(dòng)分配顏色

?

      
           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行有個(gè)bug,應(yīng)該是 >=? ,我原來寫成 > 了,博客中我改過來了,源碼我就不改了。

如上代碼,第12行就是通過GeocodingApi 獲取指定地址的物理坐標(biāo),由于有時(shí)候獲取不到坐標(biāo),有時(shí)候由于地址不確切,有多個(gè)坐標(biāo),所以我加了一個(gè)錯(cuò)誤和警告的屬性,用于調(diào)用者得到相關(guān)的信息。

_Kml 這個(gè)對(duì)象是一個(gè) Kml 類的實(shí)例,這個(gè)類用于生成 KML 文件結(jié)構(gòu),并可以保存到KML文件中。這個(gè)類在后面介紹。

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

標(biāo)注顏色這里其實(shí)還有一個(gè)問題,就是如果讓相鄰的節(jié)點(diǎn)顯示不同顏色,這個(gè)算法比較復(fù)雜了,我沒有實(shí)現(xiàn),各位如果有興趣可以思考一下這個(gè)怎么做。

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

    
      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);
        }
    }

}

  

下面看一下調(diào)用的方法,使用者如果不想仔細(xì)研究細(xì)節(jié),那就關(guān)注這個(gè)就可以了,調(diào)用方法非常簡(jiǎn)單

      
           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:  
      
          }
    
      ?
    
      第三行,實(shí)例化 KML 生成器,并指定一個(gè)名字,這個(gè)名字對(duì)于 kml 文檔中的 name 字段。
    
      第五行,在kml 文件中標(biāo)注 google 總部的地址
    
      第八行,判斷是否有最新的錯(cuò)誤,每次執(zhí)行第五行的Generate 方法,會(huì)將最新錯(cuò)誤清空,所以這里永遠(yuǎn)是得到最近一次調(diào)用 Generate 方法的錯(cuò)誤或警告。
    
      后面以此類推了。
    
      最后 Save 到一個(gè)kml文件中就OK了。
    
      ?
    
      最后,我們可以把這個(gè) kml 文件導(dǎo)入到我們自己創(chuàng)建的 google map 中。這個(gè)在 google maps 里面有相應(yīng)的導(dǎo)入功能,這里就不介紹了。
    
      ?
    
      完整源碼下載
    
      ?
    
      ?
    
      注意源碼中 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? 網(wǎng)站上獲取,地址如下:

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

?

      ?
    

C# 程序自動(dòng)批量生成 google maps 的KML文件


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 三亚市| 资阳市| 博客| 获嘉县| 奇台县| 长兴县| 腾冲县| 宕昌县| 鹤庆县| 镇巴县| 丹寨县| 湖州市| 永平县| 南城县| 肇庆市| 临澧县| 大同县| 绵阳市| 三明市| 江安县| 桃源县| 明光市| 寿阳县| 湄潭县| 诏安县| 黔西县| 长海县| 叙永县| 和平区| 阳朔县| 西乡县| 黑山县| 涡阳县| 双桥区| 琼结县| 温宿县| 武威市| 黎城县| 称多县| 揭阳市| 公主岭市|