Wednesday, January 23, 2013

Windows Phone getting Quake Info

make a project with the name QuakeML and copy the given below code to  MainPage,xaml layout file


<phone:PhoneApplicationPage
    x:Class="QuakeML.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:device="clr-namespace:System.Device.Location;assembly=System.Device"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:maps="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:cx="clr-namespace:QuakeML" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
   
    <phone:PhoneApplicationPage.Resources>
        <cx:StringFormatConverter x:Key="StringFormat"/>
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="COMPILED EXPERIENCE" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="quakes" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
           
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="125"/>
            </Grid.ColumnDefinitions>
           
            <Slider x:Name="Magnitude" Minimum="1" Maximum="8" Value="4.5" />

            <StackPanel Grid.Column="1" Orientation="Horizontal">
                <TextBlock Text="{Binding ElementName=Magnitude, Path=Value, Converter={StaticResource StringFormat}, ConverterParameter=\{0:0.0\}}" VerticalAlignment="Center" FontSize="{StaticResource PhoneFontSizeLarge}" />
                <TextBlock Text=" to 8" VerticalAlignment="Center" FontSize="{StaticResource PhoneFontSizeLarge}" />
            </StackPanel>

            <maps:Map ZoomLevel="6"  CredentialsProvider="AhlIQ8LF2Oe3_rEydhw-IZtxG6lGthdsnQpCeB_OhpM410qhMntZmvp-QPe36ElZ" Mode="Aerial" Grid.Row="1" Grid.ColumnSpan="2">
                <maps:Map.Center>
                    <device:GeoCoordinate Latitude="-43.531637" Longitude="172.636645"/>
                </maps:Map.Center>
                <maps:MapLayer x:Name="QuakeLayer"/>
            </maps:Map>
           
            <Button Content="Refresh" Grid.Row="2" Grid.ColumnSpan="2" Click="OnRefresh"/>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>


in this program i used my own Key for map services which you can replace with yours

in the very  next step please copy the given code to  MainPage.xaml.cs


using System;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Media;
using System.Xml.Linq;
using Microsoft.Phone.Controls.Maps;

namespace QuakeML
{
    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();

            Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            LoadQuakes();
        }

        private void LoadQuakes()
        {
            var webClient = new WebClient();

            webClient.OpenReadCompleted += OnOpenReadCompleted;

            var uri = String.Format("http://magma.geonet.org.nz/services/quake/quakeml/1.0.1/query?startDate=2010-09-03&endDate=2010-09-05&magnitudeLower={0:0.0}&magnitudeUpper=8", Magnitude.Value);

            webClient.OpenReadAsync(new Uri(uri, UriKind.Absolute));
        }

        private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
    var document = XDocument.Load(e.Result);

if(document.Root == null)
return;

    var xmlns = XNamespace.Get("http://quakeml.org/xmlns/quakeml/1.0");

    var events = from ev in document.Root.Descendants(xmlns + "event")
                select new
                {
Latitude = Convert.ToDouble(ev.Element(xmlns + "origin").Element(xmlns + "latitude").Element(xmlns + "value").Value),
Longitude = Convert.ToDouble(ev.Element(xmlns + "origin").Element(xmlns + "longitude").Element(xmlns + "value").Value),
Magnitude = Convert.ToDouble(ev.Element(xmlns + "magnitude").Element(xmlns + "mag").Element(xmlns + "value").Value),
                };

            QuakeLayer.Children.Clear();

    foreach(var ev in events.OrderBy(ev => ev.Magnitude))
    {
    var accentBrush = (Brush)Application.Current.Resources["PhoneAccentBrush"];

    var pin = new Pushpin
    {
    Location = new GeoCoordinate
    {
    Latitude = ev.Latitude,
    Longitude = ev.Longitude
    },
Background = accentBrush,
Content = ev.Magnitude.ToString("0.0"),
};

QuakeLayer.AddChild(pin, pin.Location);
    }
    }

        private void OnRefresh(object sender, RoutedEventArgs e)
        {
            LoadQuakes();
        }
    }
}

make a new class with name StringFormatConverter.cs and copy the given code to it


using System;
using System.Globalization;
using System.Windows.Data;

namespace QuakeML
{
    public class StringFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return String.Format(culture, parameter.ToString(), value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}



No comments:

Post a Comment