/// <summary> /// Move the tank to a new location. /// </summary> /// <param name="newPosition">Screen position to place the tank at.</param> public void Move(Vector2 newPosition) { // Gets the center point of the map displayed Vector2 positionOnMap = TileSystem.LatLongToPixelXY(viewer.CenterGeoCoordinate, viewer.ZoomLevel) - viewer.Offset; // Calculate the distance between the center of the screen and the tank's position Vector2 distance = newPosition - BingMapsViewer.ScreenCenter; Vector2 newPositionOnMap = positionOnMap + distance; // Gets the tank's geo-coordinate currentGeoPosition = TileSystem.PixelXYToLatLong(newPositionOnMap, viewer.ZoomLevel); routes.Clear(); pushPins.Clear(); targetGeoPosition = null; currentPushPin = null; currentRouteNode = null; }
/// <summary> /// DrawTank the pushpin on the screen, if the screen contains the push pin. /// </summary> public void Draw() { // Gets the center of the map displayed Vector2 center = TileSystem.LatLongToPixelXY(bingMapsViewer.CenterGeoCoordinate, bingMapsViewer.ZoomLevel); // Gets the position of the push pin on the map position = TileSystem.LatLongToPixelXY(Location, bingMapsViewer.ZoomLevel); // Calculates the distance between them Vector2 placeOnScreen = (position - center) + BingMapsViewer.ScreenCenter + bingMapsViewer.Offset; // If the pushpin is within screen bounds, draw it if (screenBounds.Contains((int)placeOnScreen.X, (int)placeOnScreen.Y)) { placeOnScreen.X -= texture.Width / 2; placeOnScreen.Y -= texture.Height; bingMapsViewer.SpriteBatch.Draw(texture, placeOnScreen, Color.White); } }
/// <summary> /// Initializes the active tile plane by requesting images centered at the specified geo-coordinate. /// </summary> /// <param name="centerCoordinate">The geo-coordinate which will serve as the center of the /// middle tile.</param> private void GetActivePlaneImages(GeoCoordinate centerCoordinate) { Vector2 centerPixelXY = TileSystem.LatLongToPixelXY(centerCoordinate, zoomLevel); int planeCenterIndex = PlaneCenterIndex; for (int xIndex = 0; xIndex < ActiveTilePlaneSize; xIndex++) { int xDelta = xIndex - planeCenterIndex; for (int yIndex = 0; yIndex < ActiveTilePlaneSize; yIndex++) { int yDelta = yIndex - planeCenterIndex; TileInformation cellInformation = activeTilePlane[xIndex, yIndex]; // Initialize or clean the active tile cube cell if (cellInformation == null) { cellInformation = new TileInformation(TileServerRequestCompleted); activeTilePlane[xIndex, yIndex] = cellInformation; } else { cellInformation.Dispose(); } // Calculate the center geo-coordinate for the current tile Vector2 tileCenterPixelXY = centerPixelXY + tileDimensions * new Vector2(xDelta, yDelta); GeoCoordinate tileCenterGeoCoordinate; try { tileCenterGeoCoordinate = TileSystem.PixelXYToLatLong(tileCenterPixelXY, zoomLevel); GetImageFromServer(cellInformation, tileCenterGeoCoordinate, zoomLevel, ViewType); } catch (ArgumentOutOfRangeException) { cellInformation.Image = unavailableImage; } } } }
/// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { // Do nothing if there is no push-pin to reach if (currentPushPin == null) { return; } // Check if the current push-pin does not have a route leading to it if (!isParsingMovment && currentPushPin != null && pushPins.Contains(currentPushPin.Value) && !IsRouteContained(routes, currentPushPin.Value.ID)) { // Gets the missing route from the server GetRouteFromServer(viewer.BingMapKey, currentGeoPosition, currentPushPin.Value.Location, currentPushPin); } else { // Update the target geo-coordinate if there is none if (routes.Count > 0 && IsRouteContained(routes, currentPushPin.Value.ID) && GetMapper(routes, currentPushPin.Value.ID).PushPinRoute.Last != currentRouteNode && targetGeoPosition == null) { LinkedList <GeoCoordinate> currentRoute = GetMapper(routes, currentPushPin.Value.ID).PushPinRoute; if (currentRoute != null && currentRoute.Count > 0) { currentRouteNode = GetMapper(routes, currentPushPin.Value.ID).PushPinRoute.First; targetGeoPosition = currentRouteNode.Value; } } if (targetGeoPosition != null && currentGeoPosition != null) { // Get the center of the map presented Vector2 center = TileSystem.LatLongToPixelXY(viewer.CenterGeoCoordinate, viewer.ZoomLevel); // Gets the position of the target on the map Vector2 position = TileSystem.LatLongToPixelXY(targetGeoPosition, viewer.ZoomLevel); // Calculate the distance which gets the position on the screen Vector2 targetPosition = (position - center) + BingMapsViewer.ScreenCenter; // Does the same to the current position of the tank position = TileSystem.LatLongToPixelXY(currentGeoPosition, viewer.ZoomLevel); Vector2 currentPosition = (position - center) + BingMapsViewer.ScreenCenter; Vector2 distanceBetweenTargetAndCurrentPosition = targetPosition - currentPosition; // If the tank has reached the target if (((distanceBetweenTargetAndCurrentPosition.X <= 1 && distanceBetweenTargetAndCurrentPosition.X >= 0) || (distanceBetweenTargetAndCurrentPosition.X >= -1 && distanceBetweenTargetAndCurrentPosition.X <= 0)) && (distanceBetweenTargetAndCurrentPosition.Y <= 1 && distanceBetweenTargetAndCurrentPosition.Y >= 0) || (distanceBetweenTargetAndCurrentPosition.Y >= -1 && distanceBetweenTargetAndCurrentPosition.Y <= 0)) { HandleTankReachedTargetPosition(); return; } currentPosition = HandleTankMovement(targetPosition, currentPosition); // Converts the current screen position to geo coordinate Vector2 dif = currentPosition - BingMapsViewer.ScreenCenter; Vector2 PositionOnMap = center + dif; currentGeoPosition = TileSystem.PixelXYToLatLong(PositionOnMap, viewer.ZoomLevel); } } base.Update(gameTime); }
/// <summary> /// Handle the input of the sample. /// </summary> private void HandleInput() { TouchCollection touchCollection = TouchPanel.GetState(); if (touchCollection.Count > 0) { TouchLocation touch = touchCollection[0]; // If the tank is moving he can not be relocated if (!tank.IsMoving) { if (touch.State == TouchLocationState.Pressed && !isDragingTank) { Rectangle toucRect = new Rectangle((int)touch.Position.X - 5, (int)touch.Position.Y - 5, 10, 10); if (tank.Bounds.Intersects(toucRect)) { isDragingTank = true; } } else if (touch.State == TouchLocationState.Moved && isDragingTank) { tank.Move(touch.Position); } else if (touch.State == TouchLocationState.Released && isDragingTank) { isDragingTank = false; } } } // Read all gesture while (TouchPanel.IsGestureAvailable) { GestureSample sample = TouchPanel.ReadGesture(); // If the search button is clicked, the button will handle it. if (serachButton.HandleInput(sample)) { return; } // If the switch button is clicked, the button will handle it. if (switchViewButton.HandleInput(sample)) { return; } // If the change mode button is clicked, the button will handle it. if (changeRotuingModeButton.HandleInput(sample)) { return; } if (tank.IsMoving) { // If the draw route button is clicked, the button will handle it. if (drawRouteButton.HandleInput(sample)) { return; } // If the clear all pushpins button is clicked, the button will handle it. if (clearPushPinsButton.HandleInput(sample)) { return; } } if (sample.GestureType == GestureType.Tap) { // Gets the position on the displayed map Vector2 positionOnMap = TileSystem.LatLongToPixelXY(bingMapsViewer.CenterGeoCoordinate, ZoomLevel) - bingMapsViewer.Offset; // Gets the distance between the gesture and the center of the screen Vector2 distance = sample.Position - BingMapsViewer.ScreenCenter; // Calculate the distance between the center of the screen and the position on the map Vector2 positionOfGesture = positionOnMap + distance; PushPin pushPin = new PushPin(bingMapsViewer, TileSystem.PixelXYToLatLong(positionOfGesture, ZoomLevel), pushPinTexture2D); pushPins.AddLast(pushPin); // Indicates to the Tank that new push pin is added tank.AddPushPin(pushPin); } else if (sample.GestureType == GestureType.FreeDrag) { if (!isDragingTank) { // Move the map when dragging bingMapsViewer.MoveByOffset(sample.Delta); } } // When an hold gesture is caught on push pin ask if wants to delete else if (sample.GestureType == GestureType.Hold) { // Creates a rectangle around the gesture Rectangle touchRectangle = new Rectangle( (int)sample.Position.X - 20, (int)sample.Position.Y - 40, 60, 100); foreach (PushPin pushPin in pushPins) { // Gets the center of the map displayed Vector2 center = TileSystem.LatLongToPixelXY(bingMapsViewer.CenterGeoCoordinate, bingMapsViewer.ZoomLevel); // Gets the position of the push pin on the map Vector2 position = TileSystem.LatLongToPixelXY(pushPin.Location, bingMapsViewer.ZoomLevel); // Calculate the distance between them in screen scale Vector2 targetPosition = (position - center) + BingMapsViewer.ScreenCenter + bingMapsViewer.Offset; // Checks if the push pin is in side the gesture rectangle if (touchRectangle.Contains(new Rectangle((int)targetPosition.X, (int)targetPosition.Y, 1, 1))) { Guide.BeginShowMessageBox("Warning!", "Are you sure you want to delete this push pin?", new List <string>() { "OK", "Cancel" }, 0, MessageBoxIcon.Alert, DeletePushPinCallBack, pushPin); return; } } } } }