Example #1
0
            /**
             * Constructor
             */
            public Map()
            {
                mBingMap = new Microsoft.Phone.Controls.Maps.Map();

                mVisibleAreaUpperLeftCorner = new System.Device.Location.GeoCoordinate();
                mVisibleAreaLowerRightCorner = new System.Device.Location.GeoCoordinate();

                View = mBingMap;

                // occurs when the map visible area is changed (on drag/scroll)
                mBingMap.MapPan += new EventHandler<MapDragEventArgs>(
                    delegate(object from, MapDragEventArgs args)
                    {
                        // update the visible area points
                        mVisibleAreaUpperLeftCorner = mBingMap.BoundingRectangle.Northwest;
                        mVisibleAreaLowerRightCorner = mBingMap.BoundingRectangle.Southeast;
                        /**
                         * 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_REGION_CHANGED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, mHandle);
                        mRuntime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    }
                );

                // occurs when the map zoom level is changed (on zoom in/out)
                mBingMap.MapZoom += new EventHandler<MapZoomEventArgs>(
                    delegate(object from, MapZoomEventArgs 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_ZOOM_LEVEL_CHANGED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, mHandle);
                        mRuntime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    }
                );
            }
        /*oblicza trasę na podstawie otrzymanych współrzędnych
         * jesli waypointów mniej niż dwa informacja
         * w przeciwnym wypadku zapisz je na mapie
        */
        public void CalculateRoute(Microsoft.Phone.Controls.Maps.Map map, GeocodeService.GeocodeResult[] coordinates, Helper.MapResultReceived croute, TravelMode tm = TravelMode.Driving)
        {
            if (coordinates.Length < 2)
            {
                MessageBox.Show("Too small number od location: you need startPoint and endpoint at least");
                return;
            }
            try
            {
                MapResponseSendHere = new Helper.MapResultReceived(croute);
                routedMap = map;
                RouteService.RouteServiceClient routeService = new RouteService.RouteServiceClient("BasicHttpBinding_IRouteService");
                routeService.Endpoint.Binding.ReceiveTimeout = timeout;
                routeService.Endpoint.Binding.SendTimeout = timeout;
                routeService.Endpoint.Binding.OpenTimeout = timeout;

                RouteRequest routeRequest = new RouteRequest();
                {
                    routeRequest.Credentials = new Microsoft.Phone.Controls.Maps.Credentials() { ApplicationId = bingMapKey };
                    routeRequest.Options = new RouteService.RouteOptions()
                    { RoutePathType = RouteService.RoutePathType.Points, Mode = tm };
                }
                routeRequest.Waypoints = new System.Collections.ObjectModel.ObservableCollection<RouteService.Waypoint>();
                foreach (GeocodeService.GeocodeResult coord in coordinates)
                {
                    if (null != coord)
                    {
                        if (coordinates[coordinates.Length - 1] != coord) //jesli to ostatni punkt ustaw: MOBICA
                            routeRequest.Waypoints.Add(GeocodeToWaypoint(coord));
                        else
                            routeRequest.Waypoints.Add(GeocodeToWaypoint(coord, "MOBICA"));
                    }
                }

                routeService.CalculateRouteCompleted += (sender, e) => CalculateRouteCompleted(sender, e);
                routeService.CalculateRouteAsync(routeRequest, coordinates.Length);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An exception occurred: " + ex.Message);

            }
        }