3D Set Sun Time

Download Samples Repository

Description

Demonstrates how to use use the sun mode

"Desktop" Available for Desktop

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.SetSunTime"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
			 xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
             mc:Ignorable="d" >
    <Grid>
        <esri:SceneView x:Name="MySceneView" AmbientLight="Gray">
            <esri:Scene>
                <esri:Scene.Surface>
                    <esri:ServiceElevationSource ServiceUri="http://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer" 
												 IsEnabled="True" ID="elevationLayer" />
                </esri:Scene.Surface>
                <esri:ArcGISTiledMapServiceLayer ID="AGOLayer" 
												 ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
                <esri:SceneLayer ID="BerlinSceneLayer" 
								 ServiceUri="http://scene.arcgis.com/arcgis/rest/services/Hosted/Buildings_Berlin/SceneServer/layers/0"/>
            </esri:Scene>
        </esri:SceneView>
        <Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Left" VerticalAlignment="Top" Width="180"
				Margin="20" Padding="20">
            <StackPanel >
                <Label Content="UTC Berlin:"/>
                <Label x:Name="AnimateSunTimeLabel"/>
                <ToggleButton x:Name="AnimateSunTimeButton" Height="30" Padding="5" Content="Animate Sun Time" Margin="0,5" Click="Animate_Click"/>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// Demonstrates how to use use the sun mode
	/// </summary>
	/// <title>3D Set Sun Time</title>
	/// <category>Scene</category>
	/// <subcategory>Mapping</subcategory>
	public partial class SetSunTime : UserControl
	{
		System.Windows.Threading.DispatcherTimer _myDispatcherTimer;
		private DateTime utcBerlin;

		public SetSunTime()
		{
			InitializeComponent();
			Initialize();
		}

		public async void Initialize()
		{
			await MySceneView.SetViewAsync(new Camera(52.4970586495449, 13.3387481843594, 739.703398887999, 320.56288091543763, 54.529512824712647));
		
			// Get the current time in Berlin
			utcBerlin = DateTime.UtcNow.AddHours(2);
			AnimateSunTimeLabel.Content = utcBerlin;

			// Set the AmbientLight and IsShadowsEnabled and then set the Sun Time
			MySceneView.AmbientLight = Colors.Gray;
			MySceneView.IsShadowsEnabled = true;
			MySceneView.SetSunTime(utcBerlin);
		}

		private void Animate_Click(object sender, RoutedEventArgs e)
		{
			string exceptionMessage = string.Empty;

			try
			{
				// Change the Content of the Animate button depending on whether it has been clicked. 
				AnimateSunTimeButton.Content = AnimateSunTimeButton.IsChecked.Value ? "Stop Animation" : "Animate Light Position";

				if (_myDispatcherTimer == null)
				{
					// Start a new Timer at 1 second intervals
					_myDispatcherTimer = new DispatcherTimer();
					_myDispatcherTimer.Interval = TimeSpan.FromSeconds(1);
					

					DateTime dt = utcBerlin;
					AnimateSunTimeLabel.Content = utcBerlin;
					int i = 0;
					MySceneView.SetSunTime(dt);
					_myDispatcherTimer.Tick += (s, p) =>
					{
						i++;
						if (i <= 24)
						{
							dt = dt.AddHours(1);
							MySceneView.SetSunTime(dt);
							AnimateSunTimeLabel.Content = dt;
						}
						else
							i = 1;
					};
					_myDispatcherTimer.Start();
				}
				else
				{
					_myDispatcherTimer.Stop();
					_myDispatcherTimer = null;
				}
			}
			catch (Exception ex)
			{
				exceptionMessage = ex.Message;
				if (_myDispatcherTimer != null)
					_myDispatcherTimer.Stop();
				_myDispatcherTimer = null;
			}

			if (!string.IsNullOrEmpty(exceptionMessage))
				MessageBox.Show(exceptionMessage, "Sample Error");
		}
	}
}
Feedback on this topic?