/// <summary>
        /// Adds point-of-interest pin to designated location
        /// </summary>
        /// <param name="pin">Point-of-interest pin instance</param>
        /// <param name="position">Latitude/longitude for pin placement</param>
        public static void AddPointOfInterestPin(this Map m, PointOfInterestPin pin, LatLong position)
        {
            MapLayer poiLayer = PoiLayer(m);
            if (poiLayer == null)
            {
                poiLayer = new MapLayer();
                m.Children.Add(poiLayer);
            }

            MapLayer.SetPositionAnchor(pin, pin.AnchorPoint);

            MapLayer.SetPosition(pin, new Location(position.Latitude, position.Longitude));
            poiLayer.Children.Add(pin);
        }
        /// <summary>
        /// Adds point-of-interest pin to designated location
        /// </summary>
        /// <param name="pin">Point-of-interest pin instance</param>
        /// <param name="position">Latitude/longitude for pin placement</param>
        public static void AddPointOfInterestPin(this Map m, PointOfInterestPin pin, LatLong position)
        {
            // add a new POI layer if needed
            MapLayer poiLayer = PoiLayer(m);
            if (poiLayer == null)
            {
                poiLayer = new MapLayer();
                m.Layers.Add(poiLayer);
            }

            // add a new overlay for the current pin
            var poiOverlay = new MapOverlay();
            poiOverlay.Content = pin;
            poiOverlay.GeoCoordinate = new GeoCoordinate(position.Latitude, position.Longitude);
            poiOverlay.PositionOrigin = pin.AnchorPoint;

            // add overlay to the layer
            poiLayer.Add(poiOverlay);
        }
        /// <summary>
        /// Positions current location indicator on map
        /// </summary>
        /// <param name="pin">Current location pin object</param>
        /// <param name="position">Latitude/longitude at which to place pin</param>
        public static void SetCurrentLocationPin(this Map m, CurrentLocationPin pin, LatLong position)
        {
            MapLayer.SetPositionAnchor(pin, pin.AnchorPoint);
            MapLayer.SetPosition(pin, new Location(position.Latitude, position.Longitude));

            if (!m.Children.Contains(pin))
                m.Children.Add(pin);

            pin.Visibility = Visibility.Visible;
        }
Пример #4
0
        /// <summary>
        /// Copy the desired portions of the deserialized model data to the view model collection of cameras
        /// </summary>
        /// <param name="model">Deserializeed result from API call</param>
        /// <param name="viewModel">Collection of view model items</param>
        /// <param name="centerLatitude">Latitude of center point of current map view</param>
        /// <param name="centerLongitude">Longitude of center point of current map view</param>
        /// <param name="maxResults">Maximum number of results to assign to view model (0 = assign all results)</param>
        /// <returns>Indicator of whether items were left out of the view model due to max size restrictions</returns>
        public static Boolean PopulateViewModel(cameras model, ObservableCollection<TomTomCameraViewModel> viewModel, LatLong centerPoint, Int32 maxResults = 0)
        {
            Int32 sequence = 0;

            // set up a staging list for applying any filters/max # of items returned, etc.
            var stagingList = new List<TomTomCameraViewModel>();

            // clear the view model first
            viewModel.Clear();

            // pull desired fields from model and insert into view model
            if (model.CameraList != null)
                foreach (var camera in
                            (from c in model.CameraList
                             select new TomTomCameraViewModel()
                                 {
                                     CameraId = c.cameraId,
                                     Name = c.cameraName,
                                     Orientation = c.orientation.Replace("Traffic closest to camera is t", "T"),
                                     RefreshRate = c.refreshRate,
                                     Position = new LatLong(c.latitude, c.longitude),
                                     DistanceFromCenter = MapUtilities.HaversineDistance(centerPoint, new LatLong(c.latitude, c.longitude))
                                 }))
                    stagingList.Add(camera);

            // apply max count if provided
            var resultsWereTruncated = (maxResults > 0) && (stagingList.Count > maxResults);
            foreach (var s in stagingList
                              .OrderBy((c) => c.DistanceFromCenter)
                              .Take(resultsWereTruncated ? maxResults : stagingList.Count))
            {
                s.Sequence = ++sequence;
                viewModel.Add(s);
            }

            return resultsWereTruncated;
        }
 public LocationSelectedEventArgs(LatLong position)
 {
     this.Position = position;
 }
 public LocationSelectedEventArgs(Double latitude, Double longitude)
 {
     this.Position = new LatLong(latitude, longitude);
 }
        /// <summary>
        /// Parses a pipe-delimited lat/long string into a class instance
        /// </summary>
        /// <param name="encodedString">String of format latitude|longitude</param>
        /// <param name="result">Resulting LatLong instance</param>
        /// <returns>True if input is valid format</returns>
        public static Boolean TryParse(String encodedString, out LatLong result)
        {
            String  latPartString;
            String  longPartString;
            double  latPartDouble;
            double  longPartDouble;

            // check for delimter
            var pipePos = encodedString.IndexOf('|');
            if (pipePos >= 0)
            {
                // extract string components
                latPartString = encodedString.Substring(0, pipePos);
                longPartString = encodedString.Substring(pipePos + 1);

                // try parsing each into doubles
                if (Double.TryParse(latPartString, out latPartDouble) && Double.TryParse(longPartString, out longPartDouble))
                {
                    result = new LatLong(latPartDouble, longPartDouble);
                    return true;
                } 
            }

            // failed to parse
            result = null;
            return false;
        }
        /// <summary>
        /// Calculate distance (in meters) between two points on the earth using the Haversine formula
        /// </summary>
        /// <param name="point1">First location as latitude/longitude pair</param>
        /// <param name="point2">Second location as latitude/longitude pair</param>
        /// <returns></returns>
        public static Double HaversineDistance(LatLong point1, LatLong point2)
        {
            Double R = 6371009;
            Double dLat = toRadians(point2.Latitude - point1.Latitude);
            Double dLon = toRadians(point2.Longitude - point1.Longitude);

            Double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * 
                Math.Cos(toRadians(point1.Latitude)) * Math.Cos(toRadians(point2.Latitude));
            return R * 2 * Math.Asin(Math.Sqrt(a));
        }
Пример #9
0
 /// <summary>
 /// Creates a new search result option for use in the SearchPane
 /// </summary>
 /// <param name="label">Text that should appear for item in SearchPane</param>
 /// <param name="latitude">Latitude of the associated point-of-interest</param>
 /// <param name="longitude">Longitude of the associated point-of-interest</param>
 public SearchResultSuggestion(String label, Double latitude, Double longitude)
 {
     Id       = Guid.NewGuid().ToString();
     Label    = label;
     Position = new LatLong(latitude, longitude);
 }
        /// <summary>
        /// Navigates map centering on given location
        /// </summary>
        /// <param name="position">Latitude/longitude point of new location (if null, current location is detected via GPS)</param>
        /// <param name="showMessage">Whether to show message informing user that location tracking is not enabled on device.</param>
        async Task GotoLocation(LatLong position, Boolean showMessage = false)
        {
            Boolean currentLocationRequested = position == null;
            try
            {
                // a null location is the cue to use geopositioning
                if (currentLocationRequested)
                {
                    try
                    {
                        Geoposition currentPosition = await _geolocator.GetGeopositionAsync();
                        position = new LatLong(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
                    }
                    catch (Exception)
                    {
                        if (showMessage)
                        {
                            MessageDialog md =
                                new MessageDialog("This application is not able to determine your current location. This can occur if your machine is operating in Airplane mode or if the GPS sensor is otherwise not operating.");
                            md.ShowAsync();
                        }
                    }
                }

                // don't assume a valid location at this point GPS/Wifi disabled may lead to a null location
                if (position != null)
                {
                    // register event handler to do work once the view has been reset
                    TheMap.ViewChangeEnded += TheMap_ViewChangeEndedWithRefreshNeeded;

                    // set pin for current location
                    if (currentLocationRequested) TheMap.SetCurrentLocationPin(_locationMarker, position);

                    // pan map to desired location with a default zoom level (when complete, ViewChangeEndedWithRefreshNeeded event will fire)
                    TheMap.SetView(new Location(position.Latitude, position.Longitude), (Double)App.Current.Resources["DefaultZoomLevel"]);
                }
            }

            // catch exception if location permission not granted
            catch (UnauthorizedAccessException)
            {
                if (showMessage)
                {
                    MessageDialog md =
                        new MessageDialog("This application has not been granted permission to capture your current location. Use the Settings charm to provide this access, then try the operation again.");
                    md.ShowAsync();
                }
            }
        }
Пример #11
0
        /// <summary>
        /// Navigates map centering on given location
        /// </summary>
        /// <param name="position">Latitude/longitude point of new location (if null, current location is detected via GPS)</param>
        /// <param name="showMessage">Whether to show message informing user that location tracking is not enabled on device.</param>
        async Task GotoLocation(LatLong position, Boolean showMessage = false)
        {
            Boolean currentLocationRequested = position == null;

            // a null location is the cue to use geopositioning
            if (currentLocationRequested)
            {
                try
                {
                    _geolocator = new Geolocator();

                    // register callback to reset (hide) the user's location, if location access is revoked while app is running
                    _geolocator.StatusChanged += (s, a) =>
                    {
                        this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                            new Windows.UI.Core.DispatchedHandler(() =>
                            {
                                if (a.Status == PositionStatus.Disabled)
                                    _locationMarker.Visibility = Visibility.Collapsed;
                            })
                        );
                    };

                    Geoposition currentPosition = await _geolocator.GetGeopositionAsync();
                    position = new LatLong(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
                }
                catch (Exception)
                {
                    // edge case for initial appearance of map when location services are turned off
                    App.InitialMapResizeHasOccurred = true;                    
                    
                    if (showMessage)
                    { 
                        MessageDialog md =
                            new MessageDialog("This application is unable to determine your current location; however, you can continue to use the other features of the application.\n\nThis condition can occur if you have turned off Location services for the application or are operating in Airplane mode. To reinstate the use of Location services, modify the Location setting on the Permissions flyout of the Settings charm.");
                        md.ShowAsync();
                    }
                }
            }

            // don't assume a valid location at this point GPS/Wifi disabled may lead to a null location
            if (position != null)
            {            
                // register event handler to do work once the view has been reset
                TheMap.ViewChangeEnded += TheMap_ViewChangeEndedWithRefreshNeeded;

                // set pin for current location
                if (currentLocationRequested) TheMap.SetCurrentLocationPin(_locationMarker, position);

                // pan map to desired location with a default zoom level (when complete, ViewChangeEndedWithRefreshNeeded event will fire)
                _noRefreshRequiredViewChange = true;
                TheMap.SetView(new Location(position.Latitude, position.Longitude), (Double)App.Current.Resources["DefaultZoomLevel"]);
            }
        }
Пример #12
0
 /// <summary>
 /// Creates a new search result option for use in the SearchPane
 /// </summary>
 /// <param name="label">Text that should appear for item in SearchPane</param>
 /// <param name="latitude">Latitude of the associated point-of-interest</param>
 /// <param name="longitude">Longitude of the associated point-of-interest</param>
 public SearchResultSuggestion(String label, Double latitude, Double longitude)
 {
     Id = Guid.NewGuid().ToString();
     Label = label;
     Position = new LatLong(latitude, longitude);
 }