Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Windows Phone 7 : Sensors - Displaying Sunset and Sunrise

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/16/2011 4:59:45 PM

1. Problem

Your aim is to obtain an application that is able to tell the user what time the sun will rise and set today in the user's location.

2. Solution

There are several ways to find out the time of sunrise and sunset (for example, through web services, libraries, and complex calculations). This recipe shows how to access and use a certain property in order to explore the potential of Location Services. There are several ways to calculate the times from latitude and longitude values, and we will let you choose which best suits your needs.

3. How It Works

You will use information about the user's position, obtained from the device, to calculate the time of sunrise and sunset, simulating with a helper class the behavior of a service to calculate them

4. The Code

To start writing code for this recipe we need two members field:

GeoCoordinateWatcher geo = new GeoCoordinateWatcher();
SuntimesHelper ssc = new SuntimesHelper();

SuntimesHelper is a class written by us, whose code is based on algorithms that are easily available on the Internet. This class exposes two methods, called GetSunRise and GetSunSet, as you can see in Figure 1.

Figure 1. Class diagram of SuntimesHelper

void geo_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (!e.Position.Location.IsUnknown)
{
bool isSunrise = false;
bool isSunset = false;

DateTime sunrise = SuntimesHelper.GetSunRise(e.Position.Location.Latitude,
e.Position.Location.Longitude, DateTime.Now, out isSunrise);
DateTime sunset = SuntimesHelper.GetSunSet(e.Position.Location.Latitude,
e.Position.Location.Longitude, DateTime.Now, out isSunset);
geo.Stop()
}
}


It is important when working with the position to point out that the applications used for localizing consume battery power. Although we may sound repetitive, it is important to provide the best possible user interaction in our applications. Therefore, remember to stop the use of GeoCoordinateWatcher when you don't need it anymore.

5. Usage

From Visual Studio 2010, deploy the application to a physical device and watch it calculating sunrise and sunset from your position.
Other -----------------
- Windows Phone 7 : Sensors - Indicating the User's Position?
- Windows Phone 7 : Sensors - Creating a Seismograph
- Developing for Windows Phone and Xbox Live : Avatars Using Render Targets
- Developing for Windows Phone and Xbox Live : Interacting with Objects
- Windows Phone 7 : Resetting a form by shaking the phone!
- Developing for Windows Phone and Xbox Live : Blending Between Animations
- Managing Gestures from the Silverlight for Windows Phone 7 Toolkit
- Windows Phone 7 : Handling Gestures in a Graphical Context Such as a Game Menu
- Developing for Windows Phone and Xbox Live : Modifying Avatar Lighting & Playing Multiple Animations
- Windows Phone 7 : Adding Gestures Management to Click-less Silverlight Controls
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server