Exemplo n.º 1
0
            /**
             * Constructor
             */
            public MapPin()
            {
                mPushpin = new Microsoft.Phone.Controls.Maps.Pushpin();
                mPushpin.Location = new System.Device.Location.GeoCoordinate();

                View = mPushpin;

                mPushpin.MouseLeftButtonDown += new MouseButtonEventHandler(
                    delegate(object from, MouseButtonEventArgs args)
                    {
                        /**
                         * post the event to MoSync runtime
                         */
                        Memory eventData = new Memory(8);
                        const int MAWidgetEventData_eventType = 0;
                        const int MAWidgetEventData_widgetHandle = 4;
                        eventData.WriteInt32(MAWidgetEventData_eventType, MoSync.Constants.MAW_EVENT_MAP_PIN_CLICKED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, mHandle);
                        mRuntime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    }
                );
            }
        void AddRoutes()
        {
            pushpinCanvas.Children.Clear();
            _routeLayer.Children.Clear();

            _finalRect = null;

            for (int i = 0; i < this.Source.Routes.Count; ++i)
            {
                Microsoft.Phone.Controls.Maps.MapPolyline line = new Microsoft.Phone.Controls.Maps.MapPolyline();
                line.Locations = new Microsoft.Phone.Controls.Maps.LocationCollection();
                Random rng = new Random();

                if (this.Source.Routes[i].RoutePoints.Count > 0)
                {
                    double maxLat = this.Source.Routes[i].RoutePoints[0].Latitude;
                    double minLat = this.Source.Routes[i].RoutePoints[0].Latitude;
                    double maxLon = this.Source.Routes[i].RoutePoints[0].Longitude;
                    double minLon = this.Source.Routes[i].RoutePoints[0].Longitude;

                    foreach (MobileSrc.Commuter.Shared.GpsLocation location in this.Source.Routes[i].RoutePoints)
                    {
                        line.Locations.Add(new System.Device.Location.GeoCoordinate(location.Latitude, location.Longitude, location.Altitude));

                        maxLat = Math.Max(maxLat, location.Latitude);
                        minLat = Math.Min(minLat, location.Latitude);
                        maxLon = Math.Max(maxLon, location.Longitude);
                        minLon = Math.Min(minLon, location.Longitude);
                    }

                    Color color = RouteColors[i % RouteColors.Count()];

                    line.Opacity = 0.65;
                    line.StrokeThickness = 5;
                    line.Visibility = System.Windows.Visibility.Visible;
                    line.Stroke = new System.Windows.Media.SolidColorBrush(color);
                    _routeLayer.Children.Add(line);
                    Microsoft.Phone.Controls.Maps.LocationRect rect = new Microsoft.Phone.Controls.Maps.LocationRect(maxLat, minLon, minLat, maxLon);

                    if (null == _finalRect)
                    {
                        _finalRect = rect;
                    }
                    else
                    {
                        _finalRect.West = Math.Min(_finalRect.West, rect.West);
                        _finalRect.North = Math.Max(_finalRect.North, rect.North);

                        _finalRect.East = Math.Max(_finalRect.East, rect.East);
                        _finalRect.South = Math.Min(_finalRect.South, rect.South);
                    }
                }
            }

            if (null != _finalRect)
            {
                AsyncTask executeTask = new AsyncTask(this.Dispatcher, () => WaitForStopDownloading(), (ex) => WaitForStopDownloadingComplete(ex));
                executeTask.Execute();
            }

            for (int i = 0; i < this.Source.Routes.Count; ++i)
            {
                if (this.Source.Routes[i].RoutePoints.Count > 0)
                {
                    Color color = RouteColors[i % RouteColors.Count()];

                    Microsoft.Phone.Controls.Maps.Pushpin pushPin = new Microsoft.Phone.Controls.Maps.Pushpin();
                    pushPin.Background = new SolidColorBrush(color);
                    pushPin.Content = this.Source.Routes[i].Name;

                    MobileSrc.Commuter.Shared.GpsLocation prevPushpinLoc = this.Source.Routes[i].RoutePoints[this.Source.Routes[i].RoutePoints.Count / 2 - 5];
                    MobileSrc.Commuter.Shared.GpsLocation pushpinLoc = this.Source.Routes[i].RoutePoints[this.Source.Routes[i].RoutePoints.Count / 2];
                    pushPin.Location = new System.Device.Location.GeoCoordinate(pushpinLoc.Latitude, pushpinLoc.Longitude);

                    string name = this.Source.Routes[i].Name;

                    Border border = new Border();
                    border.BorderBrush = new SolidColorBrush(Colors.Orange);
                    border.BorderThickness = new Thickness(0);
                    border.Background = new SolidColorBrush(Colors.White);
                    border.Width = 21;
                    border.Height = 21;
                    border.Opacity = .5;
                    border.CornerRadius = new CornerRadius(10.5);

                    pushPin.Opacity = .60;

                    Button tooltipBorder = new Button();
                    tooltipBorder.Style = (Style)LayoutRoot.Resources["PhoneTransparentButton"];
                    tooltipBorder.DataContext = this.Source.Routes[i];
                    tooltipBorder.Background = new SolidColorBrush(color);
                    tooltipBorder.FontSize = 16;
                    tooltipBorder.Foreground = new SolidColorBrush(Colors.White);
                    tooltipBorder.Content = name;
                    tooltipBorder.Padding = new Thickness(5);
                    tooltipBorder.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    tooltipBorder.Margin = new Thickness(4);
                    tooltipBorder.BorderBrush = new SolidColorBrush(Colors.White);
                    tooltipBorder.BorderThickness = new Thickness(2);

                    pushpinCanvas.Children.Add(tooltipBorder);
                }
            }
        }
        private void CalculateRouteCompleted(object sender, RouteService.CalculateRouteCompletedEventArgs e)
        {
            try
            {
                if ((e.Result.ResponseSummary.StatusCode == RouteService.ResponseStatusCode.Success) & (e.Result.Result.Legs.Count != 0))
                {
                    var markers = new List<GeoCoordinate>();
                    Microsoft.Phone.Controls.Maps.MapPolyline routeLine = SetRouteProperties();
                    int i = 1;
                    foreach (Microsoft.Phone.Controls.Maps.Platform.Location p in e.Result.Result.RoutePath.Points)
                    {
                        GeoCoordinate gc = new GeoCoordinate(p.Latitude, p.Longitude);
                        routeLine.Locations.Add(gc); //pin location is the same as start and end points
                        if ((e.Result.Result.RoutePath.Points.Count == i) || (1 == i))
                        {
                            Microsoft.Phone.Controls.Maps.Pushpin pin = new Microsoft.Phone.Controls.Maps.Pushpin();
                            pin.Location = gc;
                            if (1 == i)
                            {
                                pin.Background = new SolidColorBrush(Colors.Red);
                                pin.Content = "You are here";
                            }
                            else
                            {
                                pin.Background = new SolidColorBrush(Colors.Purple);
                                pin.Content = "MOBICA";
                            }

                            routedMap.Children.Add(pin);
                        }
                        markers.Add(gc);
                        i++;
                    }

                    if (null != routedMap)
                    {
                        routedMap.Children.Add(routeLine);
                    }

                    routedMap.ZoomLevel = 19;

                    routedMap.SetView(Microsoft.Phone.Controls.Maps.LocationRect.CreateLocationRect(markers));
                    markers.Clear();
                    MapResponseSendHere(routedMap);
                }
            }
            catch (Exception ex)
            {
                if ("No route was found for the waypoints provided." == ex.Message)
                    MessageBox.Show("You are too far away from your destination, please find another Mobica office.");
                else
                    MessageBox.Show("An exception occured: " + ex.Message);

                MapResponseSendHere(null);
            }
        }
        void AddRoutes()
        {
            pushpinCanvas.Children.Clear();
            _routeLayer.Children.Clear();

            _finalRect = null;

            for (int i = 0; i < this.Source.Routes.Count; ++i)
            {
                Microsoft.Phone.Controls.Maps.MapPolyline line = new Microsoft.Phone.Controls.Maps.MapPolyline();
                line.Locations = new Microsoft.Phone.Controls.Maps.LocationCollection();
                Random rng = new Random();

                if (this.Source.Routes[i].RoutePoints.Count > 0)
                {
                    double maxLat = this.Source.Routes[i].RoutePoints[0].Latitude;
                    double minLat = this.Source.Routes[i].RoutePoints[0].Latitude;
                    double maxLon = this.Source.Routes[i].RoutePoints[0].Longitude;
                    double minLon = this.Source.Routes[i].RoutePoints[0].Longitude;

                    foreach (MobileSrc.Commuter.Shared.GpsLocation location in this.Source.Routes[i].RoutePoints)
                    {
                        line.Locations.Add(new System.Device.Location.GeoCoordinate(location.Latitude, location.Longitude, location.Altitude));

                        maxLat = Math.Max(maxLat, location.Latitude);
                        minLat = Math.Min(minLat, location.Latitude);
                        maxLon = Math.Max(maxLon, location.Longitude);
                        minLon = Math.Min(minLon, location.Longitude);
                    }

                    Color color = RouteColors[i % RouteColors.Count()];

                    line.Opacity         = 0.65;
                    line.StrokeThickness = 5;
                    line.Visibility      = System.Windows.Visibility.Visible;
                    line.Stroke          = new System.Windows.Media.SolidColorBrush(color);
                    _routeLayer.Children.Add(line);
                    Microsoft.Phone.Controls.Maps.LocationRect rect = new Microsoft.Phone.Controls.Maps.LocationRect(maxLat, minLon, minLat, maxLon);

                    if (null == _finalRect)
                    {
                        _finalRect = rect;
                    }
                    else
                    {
                        _finalRect.West  = Math.Min(_finalRect.West, rect.West);
                        _finalRect.North = Math.Max(_finalRect.North, rect.North);

                        _finalRect.East  = Math.Max(_finalRect.East, rect.East);
                        _finalRect.South = Math.Min(_finalRect.South, rect.South);
                    }
                }
            }

            if (null != _finalRect)
            {
                AsyncTask executeTask = new AsyncTask(this.Dispatcher, () => WaitForStopDownloading(), (ex) => WaitForStopDownloadingComplete(ex));
                executeTask.Execute();
            }

            for (int i = 0; i < this.Source.Routes.Count; ++i)
            {
                if (this.Source.Routes[i].RoutePoints.Count > 0)
                {
                    Color color = RouteColors[i % RouteColors.Count()];

                    Microsoft.Phone.Controls.Maps.Pushpin pushPin = new Microsoft.Phone.Controls.Maps.Pushpin();
                    pushPin.Background = new SolidColorBrush(color);
                    pushPin.Content    = this.Source.Routes[i].Name;

                    MobileSrc.Commuter.Shared.GpsLocation prevPushpinLoc = this.Source.Routes[i].RoutePoints[this.Source.Routes[i].RoutePoints.Count / 2 - 5];
                    MobileSrc.Commuter.Shared.GpsLocation pushpinLoc     = this.Source.Routes[i].RoutePoints[this.Source.Routes[i].RoutePoints.Count / 2];
                    pushPin.Location = new System.Device.Location.GeoCoordinate(pushpinLoc.Latitude, pushpinLoc.Longitude);

                    string name = this.Source.Routes[i].Name;

                    Border border = new Border();
                    border.BorderBrush     = new SolidColorBrush(Colors.Orange);
                    border.BorderThickness = new Thickness(0);
                    border.Background      = new SolidColorBrush(Colors.White);
                    border.Width           = 21;
                    border.Height          = 21;
                    border.Opacity         = .5;
                    border.CornerRadius    = new CornerRadius(10.5);

                    pushPin.Opacity = .60;

                    Button tooltipBorder = new Button();
                    tooltipBorder.Style               = (Style)LayoutRoot.Resources["PhoneTransparentButton"];
                    tooltipBorder.DataContext         = this.Source.Routes[i];
                    tooltipBorder.Background          = new SolidColorBrush(color);
                    tooltipBorder.FontSize            = 16;
                    tooltipBorder.Foreground          = new SolidColorBrush(Colors.White);
                    tooltipBorder.Content             = name;
                    tooltipBorder.Padding             = new Thickness(5);
                    tooltipBorder.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    tooltipBorder.Margin              = new Thickness(4);
                    tooltipBorder.BorderBrush         = new SolidColorBrush(Colors.White);
                    tooltipBorder.BorderThickness     = new Thickness(2);

                    pushpinCanvas.Children.Add(tooltipBorder);
                }
            }
        }