diff --git a/Location/Location/Location.Tizen.Mobile/Location.Tizen.Mobile.cs b/Location/Location/Location.Tizen.Mobile/Location.Tizen.Mobile.cs new file mode 100755 index 0000000..e913911 --- /dev/null +++ b/Location/Location/Location.Tizen.Mobile/Location.Tizen.Mobile.cs @@ -0,0 +1,45 @@ +using Tizen.NUI; +using static Location.LocationServices; + +namespace Location.Tizen.Mobile +{ + class Program : NUIApplication + { + /// + /// This method called when the application created. + /// + protected override void OnCreate() + { + base.OnCreate(); + + Window window = Window.Instance; + window.BackgroundColor = Color.Cyan; + window.KeyEvent += OnKeyEvent; + + InitializePage page = new InitializePage(); + page.PositionUsesPivotPoint = true; + page.ParentOrigin = ParentOrigin.Center; + page.PivotPoint = PivotPoint.Center; + page.Size = new Size(window.WindowSize); + window.Add(page); + } + + public void OnKeyEvent(object sender, Window.KeyEventArgs e) + { + if (e.Key.State == Key.StateType.Down && (e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "Escape")) + { + Exit(); + } + } + + /// + /// The main entrance of the application. + /// + /// The arguments. + static void Main(string[] args) + { + var app = new Program(); + app.Run(args); + } + } +} diff --git a/Location/Location/Location.Tizen.Mobile/Location.Tizen.Mobile.csproj b/Location/Location/Location.Tizen.Mobile/Location.Tizen.Mobile.csproj new file mode 100755 index 0000000..e843915 --- /dev/null +++ b/Location/Location/Location.Tizen.Mobile/Location.Tizen.Mobile.csproj @@ -0,0 +1,32 @@ + + + + Exe + netcoreapp3.1 + + + + portable + + + None + + + + + + + + + + + + + + + Runtime + + + + + diff --git a/Location/Location/Location.Tizen.Mobile/res/bg_01.png b/Location/Location/Location.Tizen.Mobile/res/bg_01.png new file mode 100755 index 0000000..d5f888b Binary files /dev/null and b/Location/Location/Location.Tizen.Mobile/res/bg_01.png differ diff --git a/Location/Location/Location.Tizen.Mobile/shared/res/Location.Tizen.Mobile.png b/Location/Location/Location.Tizen.Mobile/shared/res/Location.Tizen.Mobile.png new file mode 100755 index 0000000..9f3cb98 Binary files /dev/null and b/Location/Location/Location.Tizen.Mobile/shared/res/Location.Tizen.Mobile.png differ diff --git a/Location/Location/Location.Tizen.Mobile/tizen-manifest.xml b/Location/Location/Location.Tizen.Mobile/tizen-manifest.xml new file mode 100755 index 0000000..7fba859 --- /dev/null +++ b/Location/Location/Location.Tizen.Mobile/tizen-manifest.xml @@ -0,0 +1,13 @@ + + + + + + Location.Tizen.Mobile.png + + + + + http://tizen.org/privilege/location + + diff --git a/Location/Location/Location/Location.csproj b/Location/Location/Location/Location.csproj new file mode 100755 index 0000000..5ff9e4b --- /dev/null +++ b/Location/Location/Location/Location.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + + + + + + + + diff --git a/Location/Location/Location/LocationServices.cs b/Location/Location/Location/LocationServices.cs new file mode 100755 index 0000000..f914d5e --- /dev/null +++ b/Location/Location/Location/LocationServices.cs @@ -0,0 +1,597 @@ +using System; +using System.Linq; + +using Tizen.Location; +using Tizen.Security; +using Tizen.NUI; +using Tizen.NUI.Components; +using Tizen.NUI.BaseComponents; + +namespace Location +{ + /// + /// Location service page + /// + public class LocationServices : ContentPage + { + /// + /// Locator instance + /// + private static Locator locator = null; + + /// + /// GpsSatellite instance + /// + private static GpsSatellite satellite = null; + + /// + /// CircleBoundary instance + /// + private static CircleBoundary circle = null; + + private static Button buttonStart; + private static Button buttonGetLocation; + private static Button buttonBoundary; + private static Button buttonTrack; + private static Button buttonSatellite; + private static Button buttonStop; + private static TextLabel textStatus; + private static TextLabel textTrack; + private static TextLabel textMessage; + private static Boolean isTrack = false; + private static Boolean isSatellite = false; + private static Boolean isBound = false; + + /// + /// Constructor + /// + public LocationServices() + { + } + + /// + /// Initialize class + /// + public class InitializePage : ContentPage + { + ButtonStyle style = new ButtonStyle() + { + Size = new Size(Window.Instance.WindowSize.Width - 40, 80), + Text = new TextLabelStyle() + { + TextColor = new Selector() + { + All = Color.White, + }, + PointSize = new Selector() + { + All = 8.0f, + } + }, + CornerRadius = new Vector4(30, 30, 30, 30), + BackgroundColor = new Color("#57C3D8"), + }; + + /// + /// Initialize page + /// + public InitializePage() + { + /// Create new Button to start location service + buttonStart = new Button(style) + { + Text = "Start location service", + }; + + /// Create new Button for getting location + buttonGetLocation = new Button(style) + { + Text = "Get Location" + }; + + /// Create new Button for boundary + buttonBoundary = new Button(style) + { + Text = "Location boundary" + }; + + /// Create new Button for route tracking + buttonTrack = new Button(style) + { + Text = "Track the route" + }; + + /// Create new Button for satellite + buttonSatellite = new Button(style) + { + Text = "Satellite information" + }; + + /// Create new Button to stop location service + buttonStop = new Button(style) + { + Text = "Stop location service" + }; + + /// Create new Label to show current status + textStatus = new TextLabel(); + textTrack = new TextLabel(); + textMessage = new TextLabel(); + textStatus.Text = "[Status]"; + + /// Content view of this page + Content = new View() + { + Size = new Size(Window.Instance.Size), + /// Set padding + Padding = new Extents(20, 20, 20, 20), + Layout = new LinearLayout() + { + LinearOrientation = LinearLayout.Orientation.Vertical, + LinearAlignment = LinearLayout.Alignment.CenterHorizontal, + CellPadding = new Size2D(20, 20), + }, + BackgroundImage = "*Resource*/bg_01.png" + }; + + /// + /// Application title + /// + Content.Add(new TextLabel + { + HorizontalAlignment = HorizontalAlignment.Center, + Text = "Location", + PointSize = 10, + }); + + /// Add buttons to the StackLayout + Content.Add(buttonStart); + Content.Add(buttonGetLocation); + Content.Add(buttonBoundary); + Content.Add(buttonTrack); + Content.Add(buttonSatellite); + Content.Add(buttonStop); + + /// Add Label to show status message + Content.Add(textStatus); + Content.Add(textMessage); + Content.Add(textTrack); + + /// Add button clicked event + buttonStart.Clicked += ButtonStartClicked; + buttonGetLocation.Clicked += ButtonGetLocationClicked; + buttonBoundary.Clicked += ButtonBoundaryClicked; + buttonTrack.Clicked += ButtonTrackClicked; + buttonSatellite.Clicked += ButtonSatelliteClicked; + buttonStop.Clicked += ButtonStopClicked; + + /// Set initial state of button enabled + buttonGetLocation.IsEnabled = false; + buttonGetLocation.BackgroundColor = new Color("#C4E4EA"); + buttonBoundary.IsEnabled = false; + buttonBoundary.BackgroundColor = new Color("#C4E4EA"); + buttonTrack.IsEnabled = false; + buttonTrack.BackgroundColor = new Color("#C4E4EA"); + buttonSatellite.IsEnabled = false; + buttonSatellite.BackgroundColor = new Color("#C4E4EA"); + buttonStop.IsEnabled = false; + buttonStop.BackgroundColor = new Color("#C4E4EA"); + + /// Check location permission + PrivilegeCheck(); + } + + /// + /// Permission check + /// + private void PrivilegeCheck() + { + try + { + /// Check location permission + CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/location"); + + switch (result) + { + case CheckResult.Allow: + break; + case CheckResult.Deny: + break; + case CheckResult.Ask: + /// Request to privacy popup + PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/location"); + break; + } + } + catch (Exception ex) + { + /// Exception handling + textStatus.Text = "[Status] Privilege : " + ex.Message; + } + } + + /// + /// Button event to start location service + /// + /// object + /// EventArgs + private void ButtonStartClicked(object sender, EventArgs e) + { + if (locator == null) + { + try + { + /// + /// Create Locator instance, sets LocationType to GPS + /// + /// This method selects the best method available at the moment. + /// This method uses Global Positioning System. + /// This method uses WiFi Positioning System. + /// This method uses passive mode. + locator = new Locator(LocationType.Gps); + + /// Create GpsSatellite instance + satellite = new GpsSatellite(locator); + + if (locator != null) + { + /// Starts the Locator which has been created using the specified method. + locator.Start(); + textStatus.Text = "[Status] Start location service, GPS searching ..."; + + /// Add ServiceStateChanged event to receive the event regarding service state + locator.ServiceStateChanged += Locator_ServiceStateChanged; + + /// Disable start button to avoid duplicated call. + buttonStart.IsEnabled = false; + buttonStart.BackgroundColor = new Color("#C4E4EA"); + + // Enable available buttons + buttonSatellite.IsEnabled = true; + buttonSatellite.BackgroundColor = new Color("#57C3D8"); + buttonStop.IsEnabled = true; + buttonStop.BackgroundColor = new Color("#57C3D8"); + } + else + { + /// Locator creation failed + textStatus.Text = "[Status] Location initialize .. Failed"; + } + } + catch (Exception ex) + { + /// Exception handling + textStatus.Text = "[Status] Location Initialize : " + ex.Message; + } + } + } + + /// + /// Event : ServiceStateChanged + /// + /// object + /// An enumeration of type LocationServiceState. + private void Locator_ServiceStateChanged(object sender, ServiceStateChangedEventArgs e) + { + /// Service is disabled. + /// Service is enabled. + if (e.ServiceState == ServiceState.Enabled) + { + /// Service state changed to Enabled + textStatus.Text = "[Status] Location service enabled"; + + /// Update button status + buttonStart.IsEnabled = false; + buttonStart.BackgroundColor = new Color("#C4E4EA"); + + /// Enable buttons to show available method + buttonTrack.IsEnabled = true; + buttonTrack.BackgroundColor = new Color("#57C3D8"); + buttonGetLocation.IsEnabled = true; + buttonGetLocation.BackgroundColor = new Color("#57C3D8"); + buttonBoundary.IsEnabled = true; + buttonBoundary.BackgroundColor = new Color("#57C3D8"); + } + else + { + /// Service state changed to Disabled + textStatus.Text = "[Status] Service disabled, GPS searching ..."; + } + } + + /// + /// Button event to get current location + /// + /// object + /// EventArgs + private void ButtonGetLocationClicked(object sender, EventArgs e) + { + try + { + /// + /// Gets the details of the location. + /// + /// The current latitude [-90.0 ~ 90.0] (degrees). + /// The current longitude [-180.0 ~ 180.0] (degrees). + /// The current altitude (meters). + /// The device speed (km/h). + /// The direction and degrees from the north. + /// The accuracy. + /// The time value when the measurement was done. + Tizen.Location.Location l = locator.GetLocation(); + textMessage.Text = "[GetLocation]" + "\n Timestamp : " + l.Timestamp + "\n Latitude : " + l.Latitude + "\n Longitude : " + l.Longitude; + } + catch (Exception ex) + { + /// Exception when location service is not available + textMessage.Text = "[GetLocation]" + "\n Exception : " + ex.Message; + } + } + + /// + /// Event : LocationChanged + /// + /// object + /// Object of the Location class. + private void Locator_LocationChanged(object sender, LocationChangedEventArgs e) + { + /// LocationChanged event invoked, + /// Available values : Latitude, Longitude, Altitude, Speed, Direction, Accuracy, Timestamp + textTrack.Text = "[Tracking] " + "\n Latitude : " + e.Location.Latitude + "\n Longitude : " + e.Location.Longitude; + } + + /// + /// Remove LocationChanged event + /// + private void CancelTrack() + { + /// Remove LocationChanged event + locator.LocationChanged -= Locator_LocationChanged; + buttonTrack.Text = "Track the route"; + isTrack = false; + } + + /// + /// Button event for location tracking + /// + /// object + /// EventArgs + private void ButtonTrackClicked(object sender, EventArgs e) + { + if (isTrack == true) + { + CancelTrack(); + textTrack.Text = ""; + } + else + { + /// Add LocationChanged event + locator.LocationChanged += Locator_LocationChanged; + buttonTrack.Text = "Cancel route tracking"; + isTrack = true; + } + } + + /// Cancel satellite information + private void CancelSatellite() + { + /// Remove SatelliteStatusUpdated event + satellite.SatelliteStatusUpdated -= Satellite_SatelliteStatusUpdated; + buttonSatellite.Text = "Satellite information"; + isSatellite = false; + } + + /// + /// Button event to receive satellite information + /// + /// object + /// EventArgs + private void ButtonSatelliteClicked(object sender, EventArgs e) + { + if (isSatellite == true) + { + /// Cancel satellite information + CancelSatellite(); + textMessage.Text = ""; + } + else + { + /// Add SatelliteStatusUpdated event + try + { + satellite.SatelliteStatusUpdated += Satellite_SatelliteStatusUpdated; + buttonSatellite.Text = "Cancel satellite information"; + isSatellite = true; + } + catch (Exception ex) + { + /// Exception handling + textMessage.Text = "[Satellite]\n Exception : " + ex.Message; + } + } + } + + /// + /// Event : SatelliteStatusUpdated + /// If InViewCount is bigger than 0, SatelliteInformation is available + /// + /// object + /// SatelliteStatusChangedEventArgs + private void Satellite_SatelliteStatusUpdated(object sender, SatelliteStatusChangedEventArgs e) + { + try + { + /// The number of active satellites. + /// The number of satellites in view. + /// The time at which the data has been extracted. + if (e.InViewCount > 0) + { + /// + /// Satellites returns values + /// ElementAt(0) used to check one of SatelliteInformation + /// + /// The azimuth value of the satellite in degrees. + /// The elevation of the satellite in meters. + /// The PRN value of the satellite. + /// The SNR value of the satellite in dB. + /// The flag signaling if the satellite is in use. + SatelliteInformation s = satellite.Satellites.ElementAt(0); + textMessage.Text = "[Satellite]" + "\n Timestamp : " + e.Timestamp + "\n Active : " + e.ActiveCount + " , Inview : " + e.InViewCount + "\n azimuth[" + s.Azimuth + "] elevation[" + s.Elevation + "] prn[" + s.Prn + "]\n snr[" + s.Snr + "] active[" + s.Active + "]"; + } + else + { + /// ActiveCount and InViewCount are available when SatelliteSttatusUpdated event invoked. + textMessage.Text = "[Satellite]" + "\n Timestamp : " + e.Timestamp + "\n Active : " + e.ActiveCount + " , Inview : " + e.InViewCount; + } + } + catch (Exception ex) + { + /// Exception when location service is not available + textMessage.Text = "[Satellite]\n Exception : " + ex.Message; + } + } + + /// Cancel boundary + private void CancelBoundary() + { + /// Remove ZoneChanged event + locator.ZoneChanged -= Locator_ZoneChanged; + buttonBoundary.Text = "Location boundary"; + isBound = false; + + /// RemoveBoundary to remove boundary method + locator.RemoveBoundary(circle); + + /// Dispose to release circle instance. + circle.Dispose(); + circle = null; + } + + /// + /// Button event to set location boundary + /// + /// object + /// EventArgs + private void ButtonBoundaryClicked(object sender, EventArgs e) + { + /// Set initial position + Coordinate center; + center.Latitude = 10.1234; + center.Longitude = 20.1234; + + /// Set radius value to detect out of boundary + double radius = 50.0; + + try + { + if (isBound == true) + { + /// Cancel boundary + CancelBoundary(); + textMessage.Text = ""; + } + else + { + buttonBoundary.Text = "Cancel location boundary"; + isBound = true; + + /// Create circle boundary + /// The coordinates which constitute the center of the circular boundary. + /// The radius value of the circular boundary. + circle = new CircleBoundary(center, radius); + + /// Add circle boundary to detect zone changed + locator.AddBoundary(circle); + + /// Add ZoneChanged event + locator.ZoneChanged += Locator_ZoneChanged; + } + } + catch (Exception ex) + { + /// Exception handling + textMessage.Text = "[Boundary]\n Exception : " + ex.Message; + } + } + + + /// + /// Event : ZoneChanged + /// + /// object + /// ZoneChangedEventArgs + private void Locator_ZoneChanged(object sender, ZoneChangedEventArgs e) + { + /// An enumeration of type BoundaryState. + /// The latitude value [-90.0 ~ 90.0] (degrees). + /// The longitude value [-180.0 ~ 180.0] (degrees). + /// The altitude value. + /// The timestamp value. + textMessage.Text = "[Boundary]\n Timestamp : " + e.Timestamp + "\n BoundState : " + e.BoundState + "\n Latitude : " + e.Latitude + "\n Longitude : " + e.Longitude; + } + + /// + /// Button event to stop location service + /// + /// object + /// EventArgs + private void ButtonStopClicked(object sender, EventArgs e) + { + try + { + if (circle != null) + { + /// Cancel boundary when stop button clicked + CancelBoundary(); + } + + if (isTrack == true) + { + /// Cancel tracking when stop button clicked + CancelTrack(); + } + + if (isSatellite == true) + { + /// Cancel satellite when stop button clicked + CancelSatellite(); + } + + /// Remove text messages + textMessage.Text = ""; + textTrack.Text = ""; + + /// Stop to location service + locator.Stop(); + + /// Dispose to release location resource + locator.Dispose(); + locator = null; + satellite = null; + + /// Enable location button after Stop() method called. + buttonStart.IsEnabled = true; + buttonStart.BackgroundColor = new Color("#57C3D8"); + + /// Disable buttons when location in not available. + buttonTrack.IsEnabled = false; + buttonTrack.BackgroundColor = new Color("#C4E4EA"); + buttonBoundary.IsEnabled = false; + buttonBoundary.BackgroundColor = new Color("#C4E4EA"); + buttonGetLocation.IsEnabled = false; + buttonGetLocation.BackgroundColor = new Color("#C4E4EA"); + buttonSatellite.IsEnabled = false; + buttonSatellite.BackgroundColor = new Color("#C4E4EA"); + buttonStop.IsEnabled = false; + buttonStop.BackgroundColor = new Color("#C4E4EA"); + textStatus.Text = "[Status] Stop location service"; + } + catch (Exception ex) + { + /// Exception handling + textMessage.Text = "[Stop]\n Exception : " + ex.Message; + } + } + } + } +} diff --git a/Location/README.md b/Location/README.md new file mode 100755 index 0000000..6aa5369 --- /dev/null +++ b/Location/README.md @@ -0,0 +1,18 @@ +# Location +This sample application demonstrates how to implement location. + +![Main page - overview](./location.png) + + +### Features +* Location information of a place with additional navigation. + +### Prerequisites + +* [Visual Studio](https://www.visualstudio.com/) - Buildtool, IDE +* [Visual Studio Tools for Tizen](https://docs.tizen.org/application/vstools/install) - Visual Studio plugin for Tizen .NET application development + +### Precondition + +* GPS should be "ON" in testing device for Location. + diff --git a/Location/location.png b/Location/location.png new file mode 100755 index 0000000..783b4e4 Binary files /dev/null and b/Location/location.png differ