Ejemplo n.º 1
0
        private async void myMap_Loaded_1(object sender, RoutedEventArgs e)
        {
            progbar.Visibility = Visibility.Visible;

            var         uri        = new Uri("ms-appx:///Files/pushpin_info.xml", UriKind.RelativeOrAbsolute);
            StorageFile sampleFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

            string metro_info = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);


            //TODO: save response once in a file and avoid further http requests
            //string metro_info = await Lastfm.geo_Metros();
            List <string> countries = new List <string>();

            using (XmlReader rd = XmlReader.Create(new StringReader(metro_info)))
            {
                try
                {
                    while (true)
                    {
                        //Metropolis m = new Metropolis();
                        rd.ReadToFollowing("metro");
                        rd.ReadToDescendant("name");
                        //m.name = rd.ReadElementContentAsString();
                        rd.ReadToNextSibling("country");
                        //m.country = rd.ReadElementContentAsString();
                        countries.Add(rd.ReadElementContentAsString());
                    }
                }
                catch (Exception) { }
            }
            IEnumerable <string> nodup  = countries.Distinct();
            List <string>        nodups = nodup.ToList();

            Globalv.AllMetros.Clear();
            foreach (string m in nodups)
            {
                Metropolis c = new Metropolis();
                c.country = m;
                Globalv.AllMetros.Add(c);
            }

            StorageFolder folder      = ApplicationData.Current.RoamingFolder;
            bool          isfilethere = true;

            try
            {
                StorageFile metrofile = await folder.GetFileAsync("GeocodedMetros.xml");
            }
            catch (FileNotFoundException) { isfilethere = false; }

            if (!isfilethere) //if file doesn't exist
            {
                HttpClient cli = new HttpClient();

                foreach (Metropolis c in Globalv.AllMetros)
                {
                    try
                    {
                        var geocoder = await cli.GetAsync(@"https://maps.googleapis.com/maps/api/geocode/json?address=" + c.country + "&sensor=false");

                        string geo_resp = await geocoder.Content.ReadAsStringAsync();

                        JObject jo = JObject.Parse(geo_resp);

                        Location l = new Location();
                        l.Latitude  = (double)jo["results"][0]["geometry"]["location"]["lat"];
                        l.Longitude = (double)jo["results"][0]["geometry"]["location"]["lng"];
                        //c.latlng = l;
                        c.lat = l.Latitude;
                        c.lon = l.Longitude;
                    }
                    catch (Exception) { }
                }

                //Write to file
                XmlSerializer serializer = new XmlSerializer(typeof(List <Metropolis>));

                //try
                //{
                StorageFile geoCodedMetroFile = await folder.CreateFileAsync("GeocodedMetros.xml", CreationCollisionOption.ReplaceExisting);

                var file = await geoCodedMetroFile.OpenAsync(FileAccessMode.ReadWrite);

                Stream outStream = Task.Run(() => file.AsStreamForWrite()).Result;

                serializer.Serialize(outStream, Globalv.AllMetros);
                //}
                //catch { }
            }
            else
            {
                StorageFile info = await folder.GetFileAsync("GeocodedMetros.xml");

                metro_info = await Windows.Storage.FileIO.ReadTextAsync(info);

                List <Metropolis> mt = new List <Metropolis>();
                using (XmlReader rd = XmlReader.Create(new StringReader(metro_info)))
                {
                    try
                    {
                        //rd.ReadToFollowing("ArrayOfMetropolis");
                        while (true)
                        {
                            //rd.ReadToFollowing("ArrayOfMetropolis");
                            //rd.ReadToFollowing("Metropolis");
                            //rd.ReadToFollowing("name");
                            rd.ReadToFollowing("country");
                            string country = rd.ReadElementContentAsString();
                            rd.ReadToFollowing("lat");
                            double lat = rd.ReadElementContentAsDouble();
                            rd.ReadToFollowing("lon");
                            double lon = rd.ReadElementContentAsDouble();

                            Metropolis m = new Metropolis();
                            m.country = country;
                            m.lat     = lat;
                            m.lon     = lon;
                            mt.Add(m);
                        }
                    }
                    catch (Exception) { }
                }

                Globalv.AllMetros = mt;
            }
            //for adding push pins to countries
            foreach (Metropolis m in Globalv.AllMetros)
            {
                Pushpin pin = new Pushpin();
                pin.Text = m.country;
                Location L = new Location(m.lat, m.lon);
                MapLayer.SetPosition(pin, L);
                myMap.Children.Add(pin);
                pin.Tapped += pin_Tapped;
            }

            progbar.Visibility = Visibility.Collapsed;
        }
Ejemplo n.º 2
0
        private async void myMap_Loaded_1(object sender, RoutedEventArgs e)
        {
            progbar.Visibility = Visibility.Visible;

            var uri = new Uri("ms-appx:///Files/pushpin_info.xml", UriKind.RelativeOrAbsolute);
            StorageFile sampleFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

            string metro_info = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);


            //TODO: save response once in a file and avoid further http requests
            //string metro_info = await Lastfm.geo_Metros();
            List<string> countries = new List<string>();
            using (XmlReader rd = XmlReader.Create(new StringReader(metro_info)))
            {
                try
                {
                    while (true)
                    {
                        //Metropolis m = new Metropolis();
                        rd.ReadToFollowing("metro");
                        rd.ReadToDescendant("name");
                        //m.name = rd.ReadElementContentAsString();
                        rd.ReadToNextSibling("country");
                        //m.country = rd.ReadElementContentAsString();
                        countries.Add(rd.ReadElementContentAsString());
                    }
                }
                catch(Exception) { }

                }
            IEnumerable<string> nodup = countries.Distinct();
            List<string> nodups = nodup.ToList();
            Globalv.AllMetros.Clear();
            foreach (string m in nodups)
            {
                Metropolis c = new Metropolis();
                c.country = m;
                Globalv.AllMetros.Add(c);
            }

            StorageFolder folder = ApplicationData.Current.RoamingFolder;
            bool isfilethere = true;
            try
            {
                StorageFile metrofile = await folder.GetFileAsync("GeocodedMetros.xml");
            }
            catch (FileNotFoundException) { isfilethere = false; }

            if (!isfilethere) //if file doesn't exist
            {
                HttpClient cli = new HttpClient();

                foreach (Metropolis c in Globalv.AllMetros)
                {
                    try
                    {
                        var geocoder = await cli.GetAsync(@"https://maps.googleapis.com/maps/api/geocode/json?address=" + c.country + "&sensor=false");
                        string geo_resp = await geocoder.Content.ReadAsStringAsync();
                        JObject jo = JObject.Parse(geo_resp);

                        Location l = new Location();
                        l.Latitude = (double)jo["results"][0]["geometry"]["location"]["lat"];
                        l.Longitude = (double)jo["results"][0]["geometry"]["location"]["lng"];
                        //c.latlng = l;
                        c.lat = l.Latitude;
                        c.lon = l.Longitude;
                    }
                    catch (Exception) { }

                }

                //Write to file
                XmlSerializer serializer = new XmlSerializer(typeof(List<Metropolis>));

                //try
                //{
                    StorageFile geoCodedMetroFile = await folder.CreateFileAsync("GeocodedMetros.xml", CreationCollisionOption.ReplaceExisting);
                    var file = await geoCodedMetroFile.OpenAsync(FileAccessMode.ReadWrite);
                    Stream outStream = Task.Run(() => file.AsStreamForWrite()).Result;

                    serializer.Serialize(outStream, Globalv.AllMetros);
                //}
                //catch { }

            }
            else
            {
                StorageFile info = await folder.GetFileAsync("GeocodedMetros.xml");
                metro_info = await Windows.Storage.FileIO.ReadTextAsync(info);

                List<Metropolis> mt = new List<Metropolis>();
                using (XmlReader rd = XmlReader.Create(new StringReader(metro_info)))
                {
                    try
                    {
                        //rd.ReadToFollowing("ArrayOfMetropolis"); 
                        while (true)
                        {
                            //rd.ReadToFollowing("ArrayOfMetropolis");
                            //rd.ReadToFollowing("Metropolis");
                            //rd.ReadToFollowing("name");
                            rd.ReadToFollowing("country");
                            string country = rd.ReadElementContentAsString();
                            rd.ReadToFollowing("lat");
                            double lat = rd.ReadElementContentAsDouble();
                            rd.ReadToFollowing("lon");
                            double lon = rd.ReadElementContentAsDouble();

                            Metropolis m = new Metropolis();
                            m.country = country;
                            m.lat = lat;
                            m.lon = lon;
                            mt.Add(m);
                        }
                    }
                    catch (Exception) { }

                }

                Globalv.AllMetros = mt;
            }
            //for adding push pins to countries
            foreach (Metropolis m in Globalv.AllMetros)
            {
                Pushpin pin = new Pushpin();
                pin.Text = m.country;
                Location L = new Location(m.lat, m.lon);
                MapLayer.SetPosition(pin, L);
                myMap.Children.Add(pin);
                pin.Tapped += pin_Tapped;
            }

            progbar.Visibility = Visibility.Collapsed;
        }