using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;

namespace GPSapp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            
        }

        void client_ConvertLonLatPtToNearestPlaceCompleted(object sender, terraService2.ConvertLonLatPtToNearestPlaceCompletedEventArgs e)
        {
            textBlock1.Text = e.Result;
            //throw new NotImplementedException();
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            GeoCoordinateWatcher mywatcher = new GeoCoordinateWatcher();
            var mypos = mywatcher.Position;

            // set default location
            double lat = 29.723065;
            double lon = -95.343573;

            // if my position is available (only work on real phone), then replace the default location
            if (!mypos.Location.IsUnknown)
            {
                lat = mypos.Location.Latitude;
                lon = mypos.Location.Longitude;
            }

            // call microsoft terra service to get location name
            terraService2.TerraServiceSoapClient client = new terraService2.TerraServiceSoapClient();
            client.ConvertLonLatPtToNearestPlaceCompleted += new EventHandler<terraService2.ConvertLonLatPtToNearestPlaceCompletedEventArgs>(client_ConvertLonLatPtToNearestPlaceCompleted);
            client.ConvertLonLatPtToNearestPlaceAsync(new terraService2.LonLatPt { Lat = lat, Lon = lon });
        }
    }
}