首页 > 解决方案 > 如何根据 xml 文件数据添加 GMap 标记?

问题描述

我想要实现的是,当用户按下“上传”按钮时,它将检索打开的xml文件的所有纬度和经度,并且在读取xml文件坐标后,地图会自动在地图上添加GMap标记.

我尝试从 xml 文件中检索纬度和经度数据并将纬度和经度文本添加到列表中,但我不确定如何将其添加到 Gmap 并在文件加载时将其显示在地图上。我不确定我是否做得对,请指导我..

这是来自表单类的按钮:

private void Form1_Load(object sender, EventArgs e)
        {
            //Bing map
            gmap.MapProvider = BingHybridMapProvider.Instance;
            GMaps.Instance.Mode = AccessMode.ServerOnly;

            //Paris, France
            gmap.Position = new PointLatLng(48.8566, 2.3522);

            gmap.ShowTileGridLines = false;
            gmap.ShowCenter = false;
            gmap.DragButton = MouseButtons.Left;
            
        }

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog opnfd = new OpenFileDialog();
            opnfd.Filter = "(*.xml) | *.xml";
            if (opnfd.ShowDialog() == DialogResult.OK)
            {
                TweetEvent tweetevent = new TweetEvent();
                tweetevent.GetLocation();                   

                GMapOverlay markers = new GMapOverlay("markers");
                GMapMarker marker = new GMarkerGoogle(
                        new PointLatLng(lat, lng), //Not sure how to add lats and lons from xml file into here
                        GMarkerGoogleType.blue_pushpin);

                gmap.Overlays.Add(markers);
                markers.Markers.Add(marker);
            }

这是 TweetEvent 类中的类函数

public override void GetLocation()
        {
            //Load xml file
            XDocument xmldoc = XDocument.Load(@"C:\Users\JACK\source\repos\LINQ_Learn\LINQ_Learn\xmlFile.xml");
            XNamespace nlle = "http://www.aab.org/lifelogevents";
            XNamespace nSoapenv = "http://www.w3.org/2001/12/soap-envelope";
      
            //Retrieve latitudes and longitudes when tweet tag is encountered
            var tweetlatlons = from tweetTag in xmldoc.Descendants(nlle + "tweet").Descendants(nlle + "location")
                               select new { Lat = tweetTag.Element(nlle + "lat").Value, Lon = tweetTag.Element(nlle + "long").Value };

            List<string> getlatslons = new List<string>();

            //Add all the lats and lons into the List
            foreach (var latslons in tweetlatlons)
            {
                string lati = latslons.Lat;
                string lngi = latslons.Lon;

                getlatslons.Add(lati);
                getlatslons.Add(lngi);
            }

        }

这是xml文件:

soapenv:Letter xmlns:soapenv="http://www.w3.org/2001/12/soap-envelope" soapenv:encodingStyle="http://www.w3.org/2001/12/soap-encoding" >
            <soapenv:Body xmlns:lle="http://www.aab.org/lifelogevents" >
                <lle:Event>
                    <lle:eventid>ID1</lle:eventid>
                    <lle:tweet>
                        <lle:text>This is some tweet in my day</lle:text>
                        <lle:location>
                            <lle:lat>123</lle:lat>
                            <lle:long>456</lle:long>
                        </lle:location>
                    </lle:tweet>                              
                </lle:Event>
                <lle:Event>
                    <lle:eventid>ID2</lle:eventid>
                        <lle:instagram-post-update>
                            <lle:text>This is some status update in my day</lle:text>
                            <lle:location>
                              <lle:lat>789</lle:lat>
                              <lle:long>987</lle:long>
                            </lle:location>
                        </lle:instagram-post-update>
                    </lle:Event>
                <lle:Event>
                    <lle:eventid>ID3</lle:eventid>
                    <lle:tweet>
                        <lle:text>This is some tweet in my day</lle:text>
                        <lle:location>
                            <lle:lat>434</lle:lat>
                            <lle:long>554</lle:long>
                        </lle:location>
                    </lle:tweet>                              
                </lle:Event>
            </soapenv:Body>
        </soapenv:Letter>

标签: c#linqgoogle-maps-markerslinq-to-xmlgmap.net

解决方案


推荐阅读