// Get map package which is neighbour for given in specified direction. public MapPackage getNeighbourMapPkg(MapPackage pkg, Point direction, int zoom) { if (direction.X != -1 && direction.X != 0 && direction.X != 1 || direction.Y != -1 && direction.Y != 0 && direction.Y != 1) { throw new MapNotFoundException("Bad direction!"); } double topLeftLongitude = pkg.getTopLeftLongitude(); double topLeftLatitude = pkg.getTopLeftLatitude(); double bottomRightLongitude = pkg.getBottomRightLongitude(); double bottomRightLatitude = pkg.getBottomRightLatitude(); double longitude = (topLeftLongitude + bottomRightLongitude) / 2; double latitude = (topLeftLatitude + bottomRightLatitude) / 2; switch (direction.X) { case 1: longitude = bottomRightLongitude + 0.000001; break; case -1: longitude = topLeftLongitude - 0.000001; break; } // NOTE: y has inverted direction in map context switch (direction.Y) { case 1: latitude = bottomRightLatitude - 0.000001; break; case -1: latitude = topLeftLatitude + 0.000001; break; } return(getMapPkg(latitude, longitude, zoom)); }